Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

SCHOOL OF COMPUTER SCIENCE AND ENGINEERING

FALL SEMESTER 2021-22


CSE5002: Operating Systems and Virtualization (Lab)

Reg. No: 21MAI0052

Name: Hari Krishna D R

Experiment Name: PROCESS MANAGEMENT


1) Demostrate the use of fork and exec with appropriate examples

CODE:

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

int main()
{
pid_t pid;
int ret = 1;
int status;
pid = fork();
printf("\nHello");

if(pid == -1)
{
printf("\n Can't create Child, Error Occured");
exit(EXIT_FAILURE);
}
else if(pid == 0)
{
printf("World");
printf("\nChild Process Created Successfully with PID: %u\n",getpid());
printf("\nParent of the Child Process just created is having PID: %u\n",getppid());
char* argv_list[] ={"ls","-lart","/home",NULL};
execv("ls",argv_list);
exit(0);
}
else
{
printf("\nParent of Parent Process having PID: %u",getppid());
printf("\nParent Process having PID: %u",getpid());

if(waitpid(pid,&status,0)>0)
{
if(WIFEXITED(status) && !WEXITSTATUS(status))
{
printf("\nPROGRAM EXECUTION IS SUCCESSFULL");
}
else if(WIFEXITED(status) && WEXITSTATUS(status))
{
if(WIFEXITED(status) == 127)
{
printf("\nexecv failed");
}
else
{
printf("\nProgram Terminated Normally,but returned a non zero status");
}
}
else
{
printf("\nProgram didn't terminate Normally");
}
}
else
{
printf("\nwaitpid() FAILED");
}
exit(0);
}
return 0;
}
2) Zombie & Orphan Process

CODE:
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
int zombie()
{
pid_t pid;
pid = fork();
printf("\nHello World with current Process ID at %u\n",getpid());
if(pid == 0)
{
printf("\nControl is in Child Process with PID and parentPID is: %u
%u\n",getpid(),getppid());
}
if(pid>0)
{
sleep(5);
printf("\nControl to Parent after sleep, Parent Process PID: %u",getpid());
}
else
{

exit(0);
}

return 0;
}

int orphan()
{
pid_t pid;
pid = fork();
printf("HELLO WORLD with Process ID %u",getpid());
if(pid == 0)
{
printf("\nChild Process with child PID: %u",getpid());
printf("\nThe Parent of this Child Process with PID is: %u\n",getppid());
kill(getppid(),SIGKILL);
printf("\nParent of Orphan Process (after killing its Parent) is : %u",getppid());
}
else if(pid > 0)
{
printf("\nControl is in Parent Process with PID: %u\n\n",getpid());
wait(NULL);
}
return 0;
}

int main()
{
int c;

printf("\n Choose Process 1.ZOMBIE PROCESS 2.ORPHAN PROCESS\n");


scanf("%d",&c);
switch(c)
{
case 1:
zombie();
break;

case 2:
orphan();
break;

default:
printf("\n Enter the valid Choice");
}
return 0;
}
3) Implement a thread to implement Matrix Multiplication. One row and one
column for each thread.
CODE:

#include<stdio.h>
#include<unistd.h>
#include<pthread.h>
#include<stdlib.h>
# define max 10
void *mult(void *arg);
int main()
{

int A[max][max], B[max][max];


int i1,j1,i2,j2;
scanf("%d %d %d %d", &i1,&j1,&i2,&j2);
for(int i=0;i<i1;i++)
{
for(int j=0;j<j1;j++)
{
scanf("%d",&A[i][j]);
}
}
for(int i=0;i<i2;i++)
{
for(int j=0;j<j2;j++)
{
scanf("%d",&B[i][j]);
}
}

int m = (i1)*(j2);

pthread_t *threads;
threads = (pthread_t*)malloc(m*sizeof(pthread_t));
int count = 0;
int *data = NULL;
for(int i=0;i<i1;i++)
{
for(int j=0;j<j2;j++)
{
data = (int*)malloc((20)*sizeof(int));
data[0] = j1;

for(int k=0;k<j1;k++)
{
data[k+1]=A[i][k];
}
for(int k=0;k<i2;k++)
{
data[k+j1+1]=B[k][j];
}

pthread_create(&threads[count++], NULL, mult, (void*)(data));


}
}

for(int i=0;i<m;i++)
{
void *k;
pthread_join(threads[i], &k);
int *p = (int*)k;
printf("%d ",*p);
}

return 0;
}

void *mult(void* arg)


{
int *data = (int*)arg;
int k=0,i=0;
int x = data[0];
for(i=1;i<=x;i++)
{
k += data[i]*data[i+x];
}
int *p = (int*)malloc(sizeof(int));
*p = k;
pthread_exit(p);
}
4) Communicate between parent and child using pipe.

{Pass a message in Parent, display the parent message in child along with its own
message}

CODE:

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
#define message 50
char *ms = "Hello World" ;
char *ms1;
int main()
{
char input[message];
int fd[2],nbytes;
pipe(fd);
if(pipe(fd) == -1)
{
printf("\nPipe is not Created\n");
exit(EXIT_FAILURE);
}
pid_t pid = fork();

if((pid > 0))


{ close(fd[0]);
printf("\n Message from Parent to child\n");
write(fd[1], ms, message);
close(fd[1]);
}
else if(pid == 0)
{ printf("\n_________IN CHILD PROCESS_________\n");
close(fd[1]);
while((nbytes = read(fd[0], input, message))>0)
{
printf("\nParent's message is %s",input);
write(fd[1], ms1, message);
printf("\nChild is Reading message.....");
close(fd[1]);
read(fd[0], input, message);
printf("\nChild message is %s\n",input);
}
if(nbytes!=0)
{
exit(2);
}
printf("Finished Reading\n");
}
else
{
printf("/nChild not Created");
}
return 0;
}

You might also like