Fork
Fork
Fork system call is used for creating a new process, which is called child process, which runs
concurrently with the process that makes the fork() call (parent process).
Program
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
fork();
printf("Hello world!\n");
return 0;
Output
wait()
A call to wait() blocks the calling process until one of its child processes exits or a signal is
received. After child process terminates, parent continues its execution after wait system call
instruction.
Program
Program
Output
exit()
In C, exit() terminates the calling process without executing the rest code which is after the
exit() function.
Program
#include <stdio.h>
#include <stdlib.h>
int main(void)
printf("START");
printf("End of program");
Output
pipe()
a pipe is a connection between two processes, such that the standard output from one process
becomes the standard input of the other process.
Program
#include<stdio.h>
#include<unistd.h>
int main() {
int pipefds[2];
int returnstatus;
char writemessages[2][20]={"Hi", "Hello"};
char readmessage[20];
returnstatus = pipe(pipefds);
if (returnstatus == -1) {
printf("Unable to create pipe\n");
return 1;
}
printf("Writing to pipe - Message 1 is %s\n", writemessages[0]);
write(pipefds[1], writemessages[0], sizeof(writemessages[0]));
read(pipefds[0], readmessage, sizeof(readmessage));
printf("Reading from pipe – Message 1 is %s\n", readmessage);
printf("Writing to pipe - Message 2 is %s\n", writemessages[0]);
write(pipefds[1], writemessages[1], sizeof(writemessages[0]));
read(pipefds[0], readmessage, sizeof(readmessage));
printf("Reading from pipe – Message 2 is %s\n", readmessage);
return 0;
}
Output
Named Pipe or FIFO
Named Pipe (FIFO) is used for communication between unrelated processes on a system.
Program
There are two programs that use the same FIFO. Program 1 writes first, then reads. The
program 2 reads first, then writes. They both keep doing it until terminated.
Writer File
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
int main()
{
int fd;
char buf[1024];
char * myfifo = "/tmp/myfifo";
return 0;
}
Reader File
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#define MAX_BUF 1024
int main()
{
int fd;
char * myfifo = "/tmp/myfifo";
char buf[MAX_BUF];
return 0;
}
Output