This snippet will show you two processes communicating between each other using a pipe.
intpfds[2];charbuf[30];/* open the pipe */pipe(pfds);if(!fork()){/* the child will write to the pipe */write(pfds[1],"test",5);exit(0);}else{/* the parent will read from the pipe */read(pfds[0],buf,5);wait(NULL);}
FIFOs are just pipes that have a specific name. In this snippet the name is a constant defined elsewhere called FIFO_NAME.
/* first program writes to the named pipe */charbuffer[20];intnum,fd;/* create the named pipe */mknod(FIFO_NAME,S_IFIFO|0666,0);/* open for writing */fd=open(FIFO_NAME,O_WRONLY);/* write to the pipe */write(fd,buffer,strlen(buffer));/* second program reads from the named pipe */charbuffer[20];intnum,fd;/* create the named pipe */mknod(FIFO_NAME,S_IFIFO|0666,0);/* open for reading */fd=open(FIFO_NAME,O_RDONLY);/* read from the pipe */read(fd,s,20);
Message queues are pretty common structures for inter-process communication. A common queue is created by one of the processes, from there it can be connected to by any other process and have messages submitted to it.
This snippet shows the creation of a queue and sending a message. Another block below this will show receiving a message from the queue.
charbuf[20];intmsqid;key_tkey;/* --- message sender --- *//* make a key */key=ftok("first",'B');/* create the message queue */msqid=msgget(key,0644|IPC_CREAT));/* put a message onto the queue */msgsnd(msqid,&buf,20,0);/* destroy (remove) the message queue */msgctl(msqid,IPC_RMID,NULL);/* --- message receiver --- *//* make a key */key=ftok("second",'B');/* connect to the message queue */msqid=msgget(key,0644);/* receive the message off the queue */msgrcv(msqid,&buf,20,0,0);
This snippet will show you how to open a file and map it into memory.
intfd,pagesize;char*data;/* open the file */fd=open("somefile",O_RDONLY);/* get the current page size */pagesize=getpagesize();/* map the 2nd page into memory */data=mmap((caddr_t)0,pagesize,PROT_READ,MAP_SHARED,fd,pagesize);/* start using the data pointer */
Locking a file helps assure your program that no other processes can tamper with it or a region of it.
This snippet will show you how to lock and unlock a file.struct flock fl;
int fd;
/* fill out the lock structure */fl.l_type=F_WRLCK;fl.l_whence=SEEK_SET;fl.l_start=0;fl.l_len=0;fl.l_pid=getpid();/* open the file */fd=open("filename",O_WRONLY);/* lock the file */fcntl(fd,F_SETLKW,&fl);/* --- complete any work here with the file locked --- *//* unlock the file now */fl.l_type=F_UNLCK;fcntl(fd,F_SETLK,&fl);
In order to create processes within the Unix environment, you must fork. Forking a process establishes the parent / child relationship which is where the waiting comes into it. All good parents wait for their children to die before terminating.
It’s just good manners, you know?
So, the snippet for this will be a fork and wait set:
/* fork execution here */pid_tpid=fork();intchild_exit_code;if(pid<0){/* -1 indicates that fork failed */exit(1);}elseif(pid==0){/* 0 indicates that this is the child process */exit(0);}else{/* the pid being returned indicates it's the parent *//* wait for the child to finish and
capture its exit code */wait(&child_exit_code);}