You are on page 1of 4

os systems...

lab

1. write a program that will show that parent and child process will not share memory location,
even global memory. how we create a child process so that the child and parent process share
their memory ?

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

int x=15;

int main()
{
int x=10,ch;
pid_t chid;
printf("1.dont share memory 2. share memory");
printf("enter your choice");
scanf("%d",&ch);
if(ch==1)
chid=fork();
else if(ch==2)
chid=vfork();
else
printf("\nwrongchoice");
if(chid==-1)
printf("error");
else if(chid==0)
{
x++; y++;
printf("child process id=%d',getpid());
printf("x=%d y=%d",x,y);
exit(0);
}
else
{
sleep(2);
printf("for parent process id=%d",getpid());
printf("x=%d y=%d",x,y);
}
return(0);
}

2. write a program that creates a chain of n processes, where n is a command-line argument.

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

int main(int argc, char *argv[])


{
pid_t chid;
int n, i;
if(argc!=2)
{
printf("argument error");
exit(0);
}
n=atoi(argv[1]);
if(n<1)
printf("argument value not proper")
else if(n==1)
printf("only one process with id %d", getpid());
else
{
for(i=2;i<n;i++)
{
chid=fork();
if(chid==-1)
printf("error");
else if(chid==0)
printf("parent process with id %d invoked child with id %d",getppid(),getpid());
else
{
wait(chid,NULL,0);
break;
}
}
}
return 0;
}

3. write a program that creates a tree of n processes, where n is a command-line argument. then
the original process should print the information after all children have exited.

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

int main(int argc, char *argv[])


{
pid_t chid;
int n, i;
if(argc!=2)
{
printf("argument error");
exit(0);
}
n=atoi(argv[1]);
if(n<1)
printf("argument value not proper")
else if(n==1)
printf("only one process with id %d", getpid());
else
{
for(i=2;i<n;i++)
{
chid=fork();
if(chid==-1)
printf("error");
else if(chid==0)
break;
else
printf("parent process with id %d invoked child with id %d",getpid(),chid);
}
}
return 0;
}

4. create zombie process.the program will show process table entry to ensure that the child
process become zombie.

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/type.h>

int main()
{
pid_t chid;
chid=fork();
if(chid==-1)
printf("error");
else if(chid==0)
printf("exiting from child with id=%d",getpid());
else
{
printf("parent process id=%d",getpid());
sleep(10);
system("ps-l");
}
return 0;
}

5. write a c program where the parent must wait for child to die and then show the exit status
status of the child process.

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

int main()
{
pid_t chid;
chid=fork();

6. write a program that creates a child process, that will replace it self with the existing system
code of "ls" let show.

7. write a program that creates a child process to execute a command. the command to be
executed is passed on the command line.
8. write a menu driven program which will display every unix commandand run on user's choice.