Professional Documents
Culture Documents
Processmanagement
Processmanagement
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;
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 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];
}
for(int i=0;i<m;i++)
{
void *k;
pthread_join(threads[i], &k);
int *p = (int*)k;
printf("%d ",*p);
}
return 0;
}
{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();