Friday, January 22, 2021

Software Tools


 

Program 1: Write command line code in Linux. 

The commands are : 1) pwd 2) ls 3) file 4) cat










Program 2: Write command line code in Linux. The commands are : 1) mkdir 2) cp 3) mv 4) rm

 


Program 3: Write command line code in Linux. The commands are : 1) clear 2) history 3) date 4) cal

 


2)History

3)Date







4)Cal









Program 4: Write a program in C to create and store information in a text file. 

 

Output:

Program 5: Write a program in C to read the file and store the lines into an array.

Output:

Program 6: Write a program in C to count a number of words and characters in a file.

Output:

Program 7: Program that uses fork to create a child process and then child uses exec to overwrites itself with a code that checks whether input received from parent is an evil number or not. A evil number is one which has even number of 1s in its binary form.

 Main program for calling fork and executing exec():

Secondary file opened in Child process using exec():

Program 8: Write a program show how a parent can avoid creation of orphan/zombie and can keep track of the terminated child in normal or abnormal condition.

Some Theory - 

Zombie state : When a process is created in UNIX using fork() system call, the address space of the Parent process is replicated. If the parent process calls wait() system call, then the execution of parent is suspended until the child is terminated. At the termination of the child, a ‘SIGCHLD’ signal is generated which is delivered to the parent by the kernel. Parent, on receipt of ‘SIGCHLD’ reaps the status of the child from the process table. Even though, the child is terminated, there is an entry in the process table corresponding to the child where the status is stored. When parent collects the status, this entry is deleted. Thus, all the traces of the child process are removed from the system. If the parent decides not to wait for the child’s termination and it executes its subsequent task, then at the termination of the child, the exit status is not read. Hence, there remains an entry in the process table even after the termination of the child. This state of the child process is known as the Zombie state.

A parent can keep track of Child process by using wait().

#include<stdio.h>

#include<unistd.h>

#include<sys/wait.h>

#include<sys/types.h>

  

int main()

{

    int i;

    int pid = fork();

    if (pid==0)

    {

        for (i=0; i<20; i++)

            printf("I am Child\n");

    }

    else

    {

        wait(NULL);

        printf("I am Parent\n");

        while(1);

    }

}


Program 9: Show the use of pipe () system call where a child displays a message that it receives from its parent through pipe.

Theory -

Conceptually, a pipe is a connection between two processes, such that the standard output from one process becomes the standard input of the other process. In UNIX Operating System, Pipes are useful for communication between related processes.

Syntax in C language:
int pipe(int fds[2]);

Parameters :

fd[0] will be the fd(file descriptor) for the 

read end of pipe.

fd[1] will be the fd for the write end of pipe.

Returns : 0 on Success.

-1 on error.


Pipes behave FIFO(First in First out), Pipe behave like a queue data structure. Size of read and write don’t have to match here. We can write 512 bytes at a time but we can read only 1 byte at a time in a pipe.

Code :- 


#include <stdio.h> 

#include <unistd.h> 

#define MSGSIZE 16 

char* msg1 = "hello, world #1"; 

char* msg2 = "hello, world #2"; 

char* msg3 = "hello, world #3"; 


int main() 

char inbuf[MSGSIZE]; 

int p[2], i; 


if (pipe(p) < 0) 

exit(1); 


/* continued */

/* write pipe */


write(p[1], msg1, MSGSIZE); 

write(p[1], msg2, MSGSIZE); 

write(p[1], msg3, MSGSIZE); 


for (i = 0; i < 3; i++) { 

/* read pipe */

read(p[0], inbuf, MSGSIZE); 

printf("% s\n", inbuf); 

return 0; 


Created by - Abhinandan Mishra

Best of Luck!


Abhinandan Mishra

Author & Editor

Dedicated to learning and making use of that learning by sharing that knowledge with everyone.

0 comments:

Post a Comment