Académique Documents
Professionnel Documents
Culture Documents
SYSTEMS
SLOT-L45-L46
Name-NALIN GUPTA
REGISTRAION NO.-18BIT0199
SUBMITTED TO- Prof. ANGULAKSHMI M
DATE - 08-08-2019
NALIN GUPTA
18BIT0199
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 -
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
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 –
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
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-
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
else
{
printf("pid %d \n",pid);
printf("Waiting on child process to finish \n");
wait(&status);
}
return 0;
}
OUTPUT –