Vous êtes sur la page 1sur 16

ITE2002-OPERATING

SYSTEMS
SLOT-L45-L46

Name-NALIN GUPTA
REGISTRAION NO.-18BIT0199
SUBMITTED TO- Prof. ANGULAKSHMI M
DATE - 08-08-2019
NALIN GUPTA
18BIT0199

Programs to illustrate various methods for process and thread


handling are:-
a) Assume that you have given a complex program that contains
large number of instructions. The program takes more time to
execute if it is executed as a single thread of execution.
Analyze the role of the system calls given below and
restructure the program using it, so that the execution time of
the program can be minimized considerably. Fork(), exec(),
getpid(), exit(), wait(), close(), stat(), opendir(), readdir().

SOLUTION: -

fork()-

CODE-
#include<stdio.h>
int main(void)
{
int fork(void),value;
value=fork();
printf("main:value =%d\n",value);
return 0;
}
OUTPUT-
NALIN GUPTA
18BIT0199

exec() –
CODE –
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
main()
{
int pid;
char *args[ ]={"/bin/ls","-l",0};
printf("\nParent Process");
pid=fork();
if(pid==0)
{
execv("/bin/ls",args);
printf("\nChild process");
}
else
{
wait();
printf("\nParent process");
exit(0);
}
}
NALIN GUPTA
18BIT0199

OUTPUT-

getpid() –
CODE –

#include<stdio.h>
int main()
{
int pid;
pid=getpid();
printf("process ID is %d\n",pid);
pid=getppid();
printf("parent process ID id %d\n",pid);
NALIN GUPTA
18BIT0199

}
OUTPUT -

wait() and exit() –


CODE-
#include<stdio.h>
#include<unistd.h>
main()
{
int i,pid;
pid=fork();
if (pid==-1)
{
perror(“fork failed”);
exit(0);
}
else if(pid==0)
{
printf(“\n Child process starts‟);
for(i=0; i<5; i++)
{
printf(“\n Child process %d is called”, i);
}
NALIN GUPTA
18BIT0199

printf(“\n Child process ends”);


}
else
{
wait(0);
printf(“\n Parent process ends”);
}
exit(0);
}
OUTPUT –

close()-
CODE –
#include<stdio.h>
#include <fcntl.h>
int main()
{
int fd1 = open("foo.txt", O_RDONLY);
if (fd1 < 0)
{
perror("c1");
NALIN GUPTA
18BIT0199

}
printf("opened the fd = % d\n", fd1);
if (close(fd1) < 0)
{
perror("c1");
}
printf("closed the fd.\n");
}
OUTPUT –

stat() –
CODE-
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdlib.h>
#include<stdio.h>
int main(void)
{
char *path,path1[10];
struct stat *nfile;
NALIN GUPTA
18BIT0199

nfile=(struct stat *) malloc (sizeof(struct stat));


printf("enter name of file whose statistics has to ");
scanf("%s",path1);
stat(path1,nfile);
printf("user id %d\n",nfile->st_uid);
printf("block size :%d\n",nfile->st_blksize);
printf("last access time %d\n",nfile->st_atime);
printf("time of last modification %d\n",nfile->st_atime);
printf("porduction mode %d \n",nfile->st_mode);
printf("size of file %d\n",nfile->st_size);
printf("number of links:%d\n",nfile->st_nlink);
}
OUTPUT-

opendir(), readdir() –
CODE –
#include<stdio.h>
#include<dirent.h>
struct dirent *dptr;
int main(int argc,char *argv[])
{
NALIN GUPTA
18BIT0199

char buff[256];
DIR *dirp;
printf("\n\nEnter directory name");
scanf("%s",buff);
if((dirp=opendir(buff))==NULL)
{
printf("Error");
exit(1);
while(dptr=readdir(dirp)){
printf("%s\n",dptr->d_name);
}
}
closedir(dirp);
}
OUTPUT –

c. Program to create processes, child processes and orphan


process.
SOLution-
Child process:
NALIN GUPTA
18BIT0199

CODE -
#include <stdio.h>
main ()
{
int pid;
printf ("I'm the original process with PID %d and PPID %d.\n", getpid (),
getppid
());
pid = fork ();
if (pid != 0)
{
printf ("I'm the parent process with PID %d and PPID %d.\n", getpid (),
getppid());
printf ("My child's PID is %d\n", pid);
}
else
{
printf ("I'm the child process with PID %d and PPID %d.\n", getpid
(),getppid());
}
printf ("PID %d terminates.\n", getpid ());
}

OUTPUT -
NALIN GUPTA
18BIT0199

Orphan process:
CODE -
#include<stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int pid = fork();
if (pid > 0)
printf("In parent process\n");
else if (pid == 0)
{
sleep(30);
printf("In child process\n");
}
return 0;
}
OUTPUT-
NALIN GUPTA
18BIT0199

d. Program to create a thread to find the factorial of a natural


number n.
Solution-

CODE :-

#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
void *sample1();
main()
{
pthread_t t1,t2;
pthread_create(&t1, NULL , &sample1, NULL);
pthread_join(t1,NULL);
}
void *sample1()
{
int n, i, f=1;
scanf(“%d”,&n);
for(i=n;i<0;i--)
{
f=f*i;
}
printf(“Factorial is %d” ,f)l
}
NALIN GUPTA
18BIT0199

OUTPUT-

e. The Collatz conjecture concerns what happens when we take any


positive
integer n and apply the
following algorithm:
n = n/2, if n is even
NALIN GUPTA
18BIT0199

n = 3 × n + 1, if n is odd
The conjecture states that when this algorithm is continually
applied, all positive
integers will eventually reach 1. For example, if n = 35, the
sequence is 35, 106,
53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1.
Write a C program using the fork () system call that generates this
sequence in the
child process. The starting number will be provided from the
command line. For
example, if 8 is passed as a parameter on the command line, the
child process will
output 8, 4, 2, 1. Because the parent and child processes have their
own copies of
the data, it will be necessary for the child to output the sequence.
Have the parent
invoke the wait () call to wait for the child process to complete
before exiting the
program. Perform necessary error checking to ensure that a
positive integer is
passed on the command line.
Solution-
CODE -
#include <stdio.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{
int number = 0;
int pid,status;
NALIN GUPTA
18BIT0199

printf("Please enter a number \n");


scanf("%d", &number);
if(number < 0)
{
printf("Please enter a positive number, greather than 0 \n");
scanf("%d", &number);
}
pid = fork();
if(pid < 0)
{
printf(" Unsuccessful to create Child Process \n");
exit(-1);
else if(pid == 0)
{
do
{
if(number%2 != 0)
{
number = (number*3)+1;
}
else if(number%2 == 0)
{
number = number/2;
}
printf("%d \n",number);
}while(number != 1);
}
NALIN GUPTA
18BIT0199

else
{
printf("pid %d \n",pid);
printf("Waiting on child process to finish \n");
wait(&status);
}
return 0;
}
OUTPUT –

Vous aimerez peut-être aussi