0% found this document useful (0 votes)
82 views

Fork

Fork creates a child process that is a copy of the parent. Wait blocks the parent process until the child exits. Exec replaces the current process image with a new program, whereas fork duplicates the process without replacing the program.

Uploaded by

Ahmad Amad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views

Fork

Fork creates a child process that is a copy of the parent. Wait blocks the parent process until the child exits. Exec replaces the current process image with a new program, whereas fork duplicates the process without replacing the program.

Uploaded by

Ahmad Amad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

fork() in C

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()

// make two process which run same

// program after this instruction

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

// C program to demonstrate working of wait()


#include<stdio.h>
#include<stdlib.h>
#include<sys/wait.h>
#include<unistd.h>
int main()
{
pid_t cpid;
if (fork()== 0)
exit(0); /* terminate child */
else
cpid = wait(NULL); /* reaping parent */
printf("Parent pid = %d\n", getpid());
printf("Child pid = %d\n", cpid);
return 0;
}
Output
exec()
fork starts a new process which is a copy of the one that calls it, while exec replaces the current
process image with another (different) one.

Program

// C program exec() system call for process creation


#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/wait.h>
int main(){
pid_t pid;
int ret = 1;
int status;
pid = fork();
if (pid == -1){
// pid == -1 means error occured
printf("can't fork, error occured\n");
exit(EXIT_FAILURE);
}
else if (pid == 0){
// pid == 0 means child process created
// getpid() returns process id of calling process
printf("child process, pid = %u\n",getpid());
char * argv_list[] = {"ls","-lart","/home",NULL};
// the execv() only return if error occured.
// The return value is -1
execv("ls",argv_list);
exit(0);
}
else{
printf("parent process, pid = %u\n",getppid());
if (waitpid(pid, &status, 0) > 0) {
if (WIFEXITED(status) && !WEXITSTATUS(status))
printf("program execution successfull\n");
else if (WIFEXITED(status) && WEXITSTATUS(status)) {
if (WEXITSTATUS(status) == 127) {
// execv failed
printf("execv failed\n");
}
else
printf("program terminated normally,"
" but returned a non-zero status\n");
}
else
printf("program didn't terminate normally\n");
}
else {
printf("waitpid() failed\n");
}
exit(0);
}
return 0;
}

Output
exit()
In C, exit() terminates the calling process without executing the rest code which is after the
exit() function.

Program

// C program to illustrate exit() function.

#include <stdio.h>

#include <stdlib.h>

int main(void)

printf("START");

exit(0); // The program is terminated here

// This line is not printed

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";

/* create the FIFO (named pipe) */


mkfifo(myfifo, 0666);
printf("Run Reader process to read the FIFO File\n");

/* write "Hi" to the FIFO */


fd = open(myfifo, O_WRONLY);
write(fd, "Hi", sizeof("Hi"));
close(fd);

/* remove the FIFO */


unlink(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];

/* open, read, and display the message from the FIFO */


fd = open(myfifo, O_RDWR);
read(fd, buf, MAX_BUF);
printf("Writer: %s\n", buf);
close(fd);

return 0;
}

Output

You might also like