Unix Simple Programs

Overview

Unix (officially trademarked as UNIX, sometimes also written as Unix with small caps) is a computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Douglas McIlroy, and Joe Ossanna. Today's Unix systems are split into various branches, developed over time by AT&T as well as various commercial vendors and non-profit organizations.

As of 2007, the owner of the trademark is The Open Group, an industry standards consortium. Only systems fully compliant with and certified to the Single UNIX Specification are qualified to use the trademark; others are called "Unix system-like" or "Unix-like".

During the late 1970s and early 1980s, the influence of Unix in academic circles led to large-scale adoption of Unix (particularly of the BSD variant, originating from the University of California, Berkeley) by commercial startups, the most notable of which are Solaris, HP-UX, AIX, and Mac OS X. Today, in addition to certified Unix systems such as those already mentioned, Unix-like operating systems such as Linux and BSD are commonly encountered. Sometimes, "traditional Unix" may be used to describe a Unix or an operating system that has the characteristics of either Version 7 Unix or UNIX System V.

Program : 1

1).fork1.c

#include<unistd.h>

#include<stdio.h>

int main (void) {

pid_tp;

p=fork();

printf(???fork returned%d\n???,p);

}

$gcc fork1.c-o fork1

$./fork1

fork returned 0

fork returned 698

1A.

#include <stdio.h>

#include <unistd.h>

int main (void){

printf(???Hello World!\n???);

fork();

printf(???\tI am process%d.\n???,getpid());

}

1B.

#include<stdio.h>

#include<unistd.h>

int main (void)){

int pid;

printf(???Hello world!\n???);

pid=fork()

if(pid=0)

printf(??? I am the child process.\n???);

else

printf(??? I am the parent process.\n???);

}

1C.

#include<stdio.h>

#include<unistd.h>

int main (void)

fork();

fork();

fork();

printf(???Hello World from process%d!\n???,getpid());

}

1D.

Guarantees the child process will print its message before the parent process.

#include<stdio.h>

#include<sys/wait.h>

int main (void){

int pid;

int status;

printf(???Hello World!\n???);

pid=fork();

if(pid==-1){

perror(???bad fork???);

exit(1);

}

if(pid==0)

printf(??? I am the child process.\n???);

else{

wait(&staurs);

printf(??? I am the parent process.\n???);

}

}

Program : 2

$ cat fork2.c

#include<stdio.h>

#include<unistd.h>

int main (void)

pid_tp;

printf(???Original program,pid=%d\n???,getpid());

p=fork();

if(p==0){

printf(??? In child process,pid=%d,ppid=%d\n???,getpid(), getppid());

}else{

printf(???In parent, pid=%d, fork returned=%d\n???,getpid(),p);

}

}

$gcc fork2.c-o fork2

$./fork2

Original program, pid=767

In child process, pid=768, ppud=767

In parent, pid=767,fork returned=768

Program : 3

$ cat exec1.c

#include<unistd.h>

#include<stdio.h>

int main (void){

/*Define a null terminated array of the command to run followed by any parameters, in this case none*/char*age[]={???/usr/bin/ls???,0};