CSE2005 Lab Da1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 25

CSE2005 – Operating System Lab Assignment 1

Submitted by: Submitted to:

Abhishek Kandel Priya M ma’am

19BCE2629

a) Shell Programming
1. Write a shell script to find the sum of first ‘N’ numbers in Fibonacci series (use for loop)

Output:
2. Write a shell script to print a given number in reverse order and sum of the individual digits.

Output:

3. Write a shell script to accept one integer argument and print its multiplication table.
4. Write a Shell Script that makes use of grep to isolate the line in /etc/passwd that contains your
login details.

Output:
5. Using shell script, display the contents of the present working directory. If it is an ordinary file
print its permission and change the permissions to r--r--r—
Output:
(b) Parent child process creation using fork( ) and exec() system call

#include<stdio.h>

#include<unistd.h>//contains fork prototype

int main(void)

printf("hello world\n");

fork( );

fork( );

printf("i am after forking\n");

printf("\t I am process %d.\n",getpid());

Output:

#include<stdio.h>
#include<unistd.h>//contains fork prototype

int main(void)

int pid;

printf("hello world\n");

printf("i am he parent process and the pid is %d.\n",getpid());

printf("here i am before use of forking\n");

pid=fork();

printf("here i am just after forking\n");

if (pid==0){

printf("i am the child process and pid is %d.\n",getpid());

else{

printf("i am the parent process and the pid is %d.\n",getpid());

Output:

#include <stdio.h>

#include <sys/types.h>
int main()

fork();

fork();

fork();

printf("hello\n");

return 0;

Output:

#include <stdio.h>

#include <sys/types.h>

#include <unistd.h>

void forkexample()

// child process because return value zero if (fork() == 0)

printf("Hello from Child!\n");

// parent process because return value non-zero. else

printf("Hello from Parent!\n");

}
int main()

forkexample();

return 0;

Output:

#include <stdio.h>

#include <sys/types.h>

#include <unistd.h>

void forkexample()

int x = 1;

if (fork() == 0)

printf("Child has x = %d\n", ++x);

else

printf("Parent has x = %d\n", --x);


}

int main()

forkexample();

return 0;

Output:

#include<stdio.h>

#include<unistd.h>

main(void){

printf("before forking\n");

fork();

printf("after first forking\n");

fork();

printf("after second forking\n");

printf("\t\thello world form process %d\n",getpid());

Output:
(c) Process and Thread Management

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h> //Header file for sleep(). man 3 sleep for details.

#include <pthread.h>

// A normal C function that is executed as a thread

// when its name is specified in pthread_create()

void *myThreadFun(void *vargp)

{
sleep(1);

printf("Printing from Thread \n");

return NULL;

int main()

pthread_t thread_id;

printf("Before Thread\n");

pthread_create(&thread_id, NULL, myThreadFun, NULL);

pthread_join(thread_id, NULL);

printf("After Thread\n");

exit(0);

Output:

Write a C program to kill a process by specifying its name rather than its PID.

#include <stdio.h>

#include <stdlib.h>

#include <sys/types.h>
#include <sys/stat.h>

#include <error.h>

#include <signal.h>

#include <unistd.h>

#include <syslog.h>

int main()

FILE *getPIDS;

char line[130];

pid_t killpid;

// setuid to that of root//

pid_t mypid = getpid();

pid_t myppid = getppid();

getPIDS = popen("pidof -x yes","r");

while (fgets(line,sizeof line,getPIDS)) {

printf("KILL PID: %s",line);

kill(line,SIGKILL);

Output:
Soln:
Output:
Solution:
Output:

You might also like