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;
}
 

C Language online compiler

Write, Run & Share C Language code online using OneCompiler's C online compiler for free. It's one of the robust, feature-rich online compilers for C language, running the latest C version which is C18. Getting started with the OneCompiler's C editor is really simple and pretty fast. The editor shows sample boilerplate code when you choose language as 'C' and start coding!

Read inputs from stdin

OneCompiler's C online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample C program which takes name as input and print your name with hello.

#include <stdio.h>
int main()
{
    char name[50];
    printf("Enter name:");
    scanf("%s", name);
    printf("Hello %s \n" , name );
    return 0;
    
}

About C

C language is one of the most popular general-purpose programming language developed by Dennis Ritchie at Bell laboratories for UNIX operating system. The initial release of C Language was in the year 1972. Most of the desktop operating systems are written in C Language.

Key features:

  • Structured Programming
  • Popular system programming language
  • UNIX, MySQL and Oracle are completely written in C.
  • Supports variety of platforms
  • Efficient and also handle low-level activities.
  • As fast as assembly language and hence used as system development language.

Syntax help

Loops

1. If-Else:

When ever you want to perform a set of operations based on a condition if-else is used.

if(conditional-expression) {
   // code
} else {
   // code
}

You can also use if-else for nested Ifs and if-else-if ladder when multiple conditions are to be performed on a single variable.

2. Switch:

Switch is an alternative to if-else-if ladder.

switch(conditional-expression) {    
case value1:    
 // code    
 break;  // optional  
case value2:    
 // code    
 break;  // optional  
...    
    
default:     
 // code to be executed when all the above cases are not matched;    
} 

3. For:

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement){  
  // code  
} 

4. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while(condition) {  
 // code 
}  

5. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {
  // code 
} while (condition); 

Arrays

Array is a collection of similar data which is stored in continuous memory addresses. Array values can be fetched using index. Index starts from 0 to size-1.

Syntax

One dimentional Array:

data-type array-name[size];

Two dimensional array:

data-type array-name[size][size];

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity.

Two types of functions are present in C

  1. Library Functions:

Library functions are the in-built functions which are declared in header files like printf(),scanf(),puts(),gets() etc.,

  1. User defined functions:

User defined functions are the ones which are written by the programmer based on the requirement.

How to declare a Function

return_type function_name(parameters);

How to call a Function

function_name (parameters)

How to define a Function

return_type function_name(parameters) {  
  //code
}