Q1

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

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

	if(pid==0)
	{
		printf("\nI am child process, id= %d",getpid());
		printf("\nPriority: %d, id= %d",nice(-7),getpid());
	}
	else
	{
		printf("\nI am parent process, id= %d",getpid());
		printf("\nPriority: %d, id= %d",nice(15),getpid());
	}
	return 0;
}

Q2

#include <stdio.h>
#include <unistd.h>
     
int main()
{
	printf("\n I am in hello.c");
	printf("\n PID of hello.c is %d",getpid());
	return 0;
}

Q3

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

int main() 
{
	printf("I am in execv_demo.c\n");
	printf("PID of execv_demo.c is %d\n",getpid());
	char* args[]={"./hello",NULL};
	execv(args[0],args); //Calling the execv() system call
	printf("Coming back to execv_demo.c");  
/*This line will not be printed if execv() runs correctly.*/
	return 0;
}

Q4

#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
int main()
{
    intpid=fork();
    if(pid>0)
    {
        printf("Parent Process:\n")
        printf("ID:%d\n\n",getpid());
    }
    else if(pid==0)
    {
        printf("Child Process\n");
        printf("ID:%d\n"getpid());
        printf("Parent -ID: %d\n\n",getppid());
        sleep(10);
        id 
        printf("\n Childe Process\n");
        printf("ID:%d\n",getpid());
        printf("Parent -ID:%d\n",getppid());
    }
    else
    {
        printf("Failed to create childe process")
    }
    return 0;
}

Q5

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

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

	if(pid==0)
	{
		printf("\nI am child process, id= %d",getpid());
		printf("\nPriority: %d, id= %d",nice(-7),getpid());
	}
	else
	{
		printf("\nI am parent process, id= %d",getpid());
		printf("\nPriority: %d, id= %d",nice(15),getpid());
	}
	return 0;
}


Q6

#include <time.h>
#include <stdio.h>

int main () {
   clock_t start_t, end_t;
   double total_t;
   int i;

   start_t = clock();
   printf("Starting of the program, start_t = %ld\n", start_t);
    
   printf("Going to scan a big loop, start_t = %ld\n", start_t);
   for(i=0; i< 10000000; i++) {
   }
   end_t = clock();
   printf("End of the big loop, end_t = %ld\n", end_t);
   
   total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC;
   printf("Total time taken by CPU: %f\n", total_t  );
   printf("Exiting of the program...\n");

   return(0);
}

Q7

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

int main()
{
	pid_t p;   /*The returned process ID is of type pid_t defined in <sys/types.h>. Normally, the process ID is an integer.*/
	
      printf("\n Before fork\n");
	p=fork();

	if(p==0) //child process
	{
	printf("\n Hello World");
	printf("\n I am child having id %d",getpid());  
      /*getpid() will return process id of child process*/
	printf("\n My parent's id is %d\n",getppid()); 
     /*getppid() will return parent process id of child     process*/
	}
	else //parent process
	{
	printf("\n Hi");
	printf("\n My child's id is %d",p);
	printf("\n I am parent having id %d\n",getpid());                  //getpid() returns process id
	}
        return 0;
}

Q8

#include <stdio.h>
int p, r, i, j, k;
int alloc[5][5],max[5][5],need[5][5];
int main()
{
printf("\nEnter the number of processes: ");
scanf("%d", &p);
printf("\nEnter the number of resources: ");
scanf("%d", &r);

printf("\nEnter allocated resource table:\n");
    for (i = 0; i < p; i++) {
        for(j = 0; j < r; j++) {
            scanf("%d", &alloc[i][j]);
        }
    }

printf("\nEnter maximum resource table:\n");
    for (i = 0; i < p; i++) {
        for(j = 0; j < r; j++) {
            scanf("%d", &max[i][j]);
        }
    }

printf("\nThe allocated resource table:\n");
    for (i = 0; i < p; i++) {
        for (j = 0; j < r; j++) {
            printf("\t%d", alloc[i][j]);
        }
        printf("\n");
    }
    
    printf("\nThe maximum resource table:\n");
    for (i = 0; i < p; i++) {
        for (j = 0; j < r; j++) {
            printf("\t%d", max[i][j]);
        }
        printf("\n");
    }
    
printf("\nThe need matrix content:\n");
for (i = 0; i < p; i++)  {  
    for (j = 0; j < r; j++)  {
    need[i][j] = max[i][j] - alloc[i][j]; 
    printf("\t%d", need[i][j]);
    }
    printf("\n");
}  
return 0;
}

Q9

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

int main()
{
	pid_t p;   /*The returned process ID is of type pid_t defined in <sys/types.h>. Normally, the process ID is an integer.*/
	
      printf("\n Before fork\n");
	p=fork();

	if(p==0) //child process
	{
	printf("\n Hello World");
	printf("\n I am child having id %d",getpid());  
      /*getpid() will return process id of child process*/
	printf("\n My parent's id is %d\n",getppid()); 
     /*getppid() will return parent process id of child     process*/
	}
	else //parent process
	{
	printf("\n Hi");
	printf("\n My child's id is %d",p);
	printf("\n I am parent having id %d\n",getpid());                  //getpid() returns process id
	}
        return 0;
}

Q10

#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
int main()
{
    intpid=fork();
    if(pid>0)
    {
        printf("Parent Process:\n")
        printf("ID:%d\n\n",getpid());
    }
    else if(pid==0)
    {
        printf("Child Process\n");
        printf("ID:%d\n"getpid());
        printf("Parent -ID: %d\n\n",getppid());
        sleep(10);
        id 
        printf("\n Childe Process\n");
        printf("ID:%d\n",getpid());
        printf("Parent -ID:%d\n",getppid());
    }
    else
    {
        printf("Failed to create childe process")
    }
    return 0;
}

Q11

#include <stdio.h>
#include <unistd.h>
     
int main()
{
	printf("\n I am in hello.c");
	printf("\n PID of hello.c is %d",getpid());
	return 0;
}

Q12

#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
int main()
{
    intpid=fork();
    if(pid>0)
    {
        printf("Parent Process:\n")
        printf("ID:%d\n\n",getpid());
    }
    else if(pid==0)
    {
        printf("Child Process\n");
        printf("ID:%d\n"getpid());
        printf("Parent -ID: %d\n\n",getppid());
        sleep(10);
        id 
        printf("\n Childe Process\n");
        printf("ID:%d\n",getpid());
        printf("Parent -ID:%d\n",getppid());
    }
    else
    {
        printf("Failed to create childe process")
    }
    return 0;
}


Q13

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

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

	if(pid==0)
	{
		printf("\nI am child process, id= %d",getpid());
		printf("\nPriority: %d, id= %d",nice(-7),getpid());
	}
	else
	{
		printf("\nI am parent process, id= %d",getpid());
		printf("\nPriority: %d, id= %d",nice(15),getpid());
	}
	return 0;
}

Q14

#include <time.h>
#include <stdio.h>

int main () {
   clock_t start_t, end_t;
   double total_t;
   int i;

   start_t = clock();
   printf("Starting of the program, start_t = %ld\n", start_t);
    
   printf("Going to scan a big loop, start_t = %ld\n", start_t);
   for(i=0; i< 10000000; i++) {
   }
   end_t = clock();
   printf("End of the big loop, end_t = %ld\n", end_t);
   
   total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC;
   printf("Total time taken by CPU: %f\n", total_t  );
   printf("Exiting of the program...\n");

   return(0);
}

Q15


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

int main()
{
	pid_t p;   /*The returned process ID is of type pid_t defined in <sys/types.h>. Normally, the process ID is an integer.*/
	
      printf("\n Before fork\n");
	p=fork();

	if(p==0) //child process
	{
	printf("\n Hello World");
	printf("\n I am child having id %d",getpid());  
      /*getpid() will return process id of child process*/
	printf("\n My parent's id is %d\n",getppid()); 
     /*getppid() will return parent process id of child     process*/
	}
	else //parent process
	{
	printf("\n Hi");
	printf("\n My child's id is %d",p);
	printf("\n I am parent having id %d\n",getpid());                  //getpid() returns process id
	}
        return 0;
}


Q16

#include <time.h>
#include <stdio.h>

int main () {
   clock_t start_t, end_t;
   double total_t;
   int i;

   start_t = clock();
   printf("Starting of the program, start_t = %ld\n", start_t);
    
   printf("Going to scan a big loop, start_t = %ld\n", start_t);
   for(i=0; i< 10000000; i++) {
   }
   end_t = clock();
   printf("End of the big loop, end_t = %ld\n", end_t);
   
   total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC;
   printf("Total time taken by CPU: %f\n", total_t  );
   printf("Exiting of the program...\n");

   return(0);
}

Q17

#include <stdio.h>

int main()
{
    int process = 3, need = 4, Resources = 0;
    
    // Condition so that deadlock will not occur
    
    Resources = process * (need - 1) + 1;
    
    printf("Minimum Resources to avoid deadlock: %d",   Resources);
    return 0;
}

Q18

#include <stdio.h>
int p, r, i, j, k;
int alloc[5][5],max[5][5],need[5][5];
int main()
{
printf("\nEnter the number of processes: ");
scanf("%d", &p);
printf("\nEnter the number of resources: ");
scanf("%d", &r);

printf("\nEnter allocated resource table:\n");
    for (i = 0; i < p; i++) {
        for(j = 0; j < r; j++) {
            scanf("%d", &alloc[i][j]);
        }
    }

printf("\nEnter maximum resource table:\n");
    for (i = 0; i < p; i++) {
        for(j = 0; j < r; j++) {
            scanf("%d", &max[i][j]);
        }
    }

printf("\nThe allocated resource table:\n");
    for (i = 0; i < p; i++) {
        for (j = 0; j < r; j++) {
            printf("\t%d", alloc[i][j]);
        }
        printf("\n");
    }
    
    printf("\nThe maximum resource table:\n");
    for (i = 0; i < p; i++) {
        for (j = 0; j < r; j++) {
            printf("\t%d", max[i][j]);
        }
        printf("\n");
    }
    
printf("\nThe need matrix content:\n");
for (i = 0; i < p; i++)  {  
    for (j = 0; j < r; j++)  {
    need[i][j] = max[i][j] - alloc[i][j]; 
    printf("\t%d", need[i][j]);
    }
    printf("\n");
}  
return 0;
}

Q19

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

int main()
{
	pid_t p;   /*The returned process ID is of type pid_t defined in <sys/types.h>. Normally, the process ID is an integer.*/
	
      printf("\n Before fork\n");
	p=fork();

	if(p==0) //child process
	{
	printf("\n Hello World");
	printf("\n I am child having id %d",getpid());  
      /*getpid() will return process id of child process*/
	printf("\n My parent's id is %d\n",getppid()); 
     /*getppid() will return parent process id of child     process*/
	}
	else //parent process
	{
	printf("\n Hi");
	printf("\n My child's id is %d",p);
	printf("\n I am parent having id %d\n",getpid());                  //getpid() returns process id
	}
        return 0;
}


Q20

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

int main()
{
	pid_t p;   /*The returned process ID is of type pid_t defined in <sys/types.h>. Normally, the process ID is an integer.*/
	
      printf("\n Before fork\n");
	p=fork();

	if(p==0) //child process
	{
	printf("\n Hello World");
	printf("\n I am child having id %d",getpid());  
      /*getpid() will return process id of child process*/
	printf("\n My parent's id is %d\n",getppid()); 
     /*getppid() will return parent process id of child     process*/
	}
	else //parent process
	{
	printf("\n Hi");
	printf("\n My child's id is %d",p);
	printf("\n I am parent having id %d\n",getpid());                  //getpid() returns process id
	}
        return 0;
}