Lab09: Coding in C (Fork System Call) Objectives

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 5

Lab09: Coding in C ( fork() system call )

Objectives
To learn how to execute and use a system call

1. C language

The C programming language is an excellent choice for beginning programmers as well as for people
who do not intend to become a programmer but just want the experience of creating a program.
This is because it is relatively simple, yet powerful and widely used. It is also because C is the basis
for many other programming languages, and thus experience gained with C can be applied to those
languages as well. In addition, experience with C is useful for obtaining an in-depth understanding
of Linux and other Unix-like operating systems, because they are largely written in C.

Every C program has exactly one function named main(). A function is a set of one or
more statements that are enclosed in curly brackets, perform some operation and return a single
value to the program in which they reside; it could also be looked at as a subprogram. The main()
function is the starting point of any program; that is, it is where the program begins execution (i.e.,
running); any other functions are subsequently executed.

Example code:

#include <stdio.h>
/* This is a comment */
int main()
{
printf("Hello, world!\n");
return 0;
}
Compilation and execution

$ gcc - o hello hello.c

$ ./hello

2. System call

System calls acts as entry point to OS kernel. There are certain tasks that can only be done if a
process is running in kernel mode. Examples of these tasks can be interacting with hardware etc. So
if a process wants to do such kind of task then it would require itself to be running in kernel mode
which is made possible by system calls.
BU,CS DEPARTMENT LAB-09

no sys_call Purpose source

1 write write to a file descriptor fs/read_write.c

2 open open and possibly create a file or device fs/open.c

3 close close a file descriptor fs/open.c

4 stat get file status fs/stat.c

5 getpid get process identification kernel/sys.c

6 exit terminate the current process kernel/exit.c

7 fork creates a child process kernel/fork.c

8 execlp used to execute a command kernel/execlp.

3. fork() System call


fork() creates a child process that differs from the parent process only in its PID and PPID, and in
the fact that resource utilizations are set to 0.

Implementation
BU,CS DEPARTMENT LAB-09

#include <sys/types.h>
#include <unistd.h>

pid_t fork(void);

On success, the PID of the child process is returned in the parent’s thread of execution, and a 0 is
returned in the child’s thread of execution. On failure, a -1 will be returned in the parent’s context, no
child process will be created, and errno will be set appropriately.

4. execlp() System call


The workaround to passing the full path of the executable is to use the functon execlp. As the
executable is searched for in the path specified by the variable PATH, if the path to the executable is
added to PATH it will be executed by execlp,thus any custom command or script can also be
launched.

Definition:

int execlp(const char *file, const char *arg, ...);

#include<stdio.h>

#include<stdlib.h>

#include<unistd.h>

main() {

char *temp1,*temp2;

temp1="Hello";

temp2="world";

execlp("echo","echo",temp1,temp2,NULL);

printf("Error");

execlp(“/bin/ls” , “ps”, NULL);

5. Creating a child process


/* Refernce: http://www.amparo.net/ce155/fork-ex.html*/
/* Includes */
BU,CS DEPARTMENT LAB-09
#include <unistd.h> /* Symbolic Constants */

#include <sys/types.h> /* Primitive System Data Types */

#include <errno.h> /* Errors */

#include <stdio.h> /* Input/Output */

#include <sys/wait.h> /* Wait for Process Termination */

#include <stdlib.h> /* General Utilities */

int main()
{
pid_t childpid; /* variable to store the child's pid */
int retval; /* child process: user-provided return code */
int status; /* parent process: child's exit status */

/* only 1 int variable is needed because each process would have its
own instance of the variable
here, 2 int variables are used for clarity */

/* now create new process */


childpid = fork();

if (childpid >= 0) /* fork succeeded */


{
if (childpid == 0) /* fork() returns 0 to the child process */
{
printf("CHILD: I am the child process!\n");
printf("CHILD: Here's my PID: %d\n", getpid());
printf("CHILD: My parent's PID is: %d\n", getppid());
printf("CHILD: The value of my copy of childpid is: %d\n",
childpid);
printf("CHILD: Sleeping for 1 second...\n");
sleep(1); /* sleep for 1 second */
printf("CHILD: Enter an exit value (0 to 255): ");
scanf(" %d", &retval);
printf("CHILD: Goodbye!\n");
exit(retval); /* child exits with user-provided return code */
}
else /* fork() returns new pid to the parent process */
{
printf("PARENT: I am the parent process!\n");
printf("PARENT: Here's my PID: %d\n", getpid());
printf("PARENT: The value of my copy of childpid is %d\n",
childpid);
printf("PARENT: I will now wait for my child to exit.\n");
wait(&status); /* wait for child to exit, and store its status */
printf("PARENT: Child's exit code is: %d\n", WEXITSTATUS(status));
printf("PARENT: Goodbye!\n");
exit(0); /* parent exits */
}
}
else /* fork returns -1 on failure */
{
perror("fork"); /* display error message */
exit(0);
}
}
BU,CS DEPARTMENT LAB-09

EXERCISES

Exercises

1. Write a program to create a child process.


2. Write a program that displays all processes running in the system.
3. Write a program for SHELL (Hint: that is when executed can run any shell command.)
4. Write a code for Zombie process.
5. Run the given code and explain the output.
.
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
int main (void)
{
int status;
pid_t pid;
if (!fork ())
return 1;
pid = wait (&status);
if (pid == −1)
perror ("wait");
printf ("pid=%d\n", pid);
if (WIFEXITED (status))
printf ("Normal termination with exit status=%d\n",
WEXITSTATUS (status));
if (WIFSIGNALED (status))
printf ("Killed by signal=%d%s\n",
WTERMSIG (status),
WCOREDUMP (status) ? " (dumped core)" : "");
if (WIFSTOPPED (status))
printf ("Stopped by signal=%d\n",
WSTOPSIG (status));
if (WIFCONTINUED (status))
printf ("Continued\n");
return 0;
}

You might also like