Articles‎ > ‎Technical Articles‎ > ‎

Unix Simple Programs

posted Aug 18, 2009, 1:17 PM by Thiyagaraaj M   [ updated Aug 18, 2009, 3:51 AM ]

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};
 
                        /*fork, and exec within child process*/
                        if (fork()==0) {
                                    printf(“ In child process:\n”);
                                    execv(arg[0], arg);
                                    printf(“I will never be called\n”);
                        }
 
                                    printf(“Execution continues in parent process\n”);
                        }
                        $gcc exec1.c-o exec1
                        $./exec1
                        In child process:
                        Fork1.c            exec1               fork2    exec1.c            fork1
                        Fork2.c
                        Execution continues in parent process
 

Program : 4

           #include<stdio.h>
            #include<strings.h>
            #include<unistd.h>
            #include<sys/types.h>
#include<sys/stat.h>
            #include<fcnt1.h>
            int main (void){
           
                        int fd_in, fd_out;
                        char buf[1024];
                        memset(buf,0,1024);
                        }
                        sleep(10);
}
 
            $gcc fdtest1.c-o fdtest1
            $./fdtest1
2875:1
2874:2
2875:3
2874:4
2875:5
2874:6
2874:7
            Sunbox$cat/tmp/outfile
            2875Hello,world!
            2874Hello,world!
            2875Hello,world!
            2874Hello,world!
            2875Hello,world!
            2874Hello,world!
            2874Hello,world!
 

Program : 5

 #include<unistd.h>
#include<stdio.h>
int main(void){
            int i;
if (fork()){
                        /*Parent*/
                        sleep(2)
                        _exit(0);
}
for(i=0;i<5;i++){
            printf(“My parent is %d\n”,getppid());
            sleep(1);
}
            }
            $gcc die1.c-o die1
            $./die1
            My parent is 2920
            My parent is 2920
            Sunbox$ My parent is 1
            My parent is 1
            My parent is 1
 

Program : 6

         $cat die2.c
      #include<unistd.h>
#include<stdio.h>
int main(void){
                        int i;
                        if (!fork()){
                                    /*Child exists immediately*/
                                    _exit(0);
                        }
                                    /*Parent waits around for a minute*/
                        sleep(60);
            }
 
            $gcc die2.c-o die2
            $./die2&
            [1] 2934
            $pa-ef|grep 2934
                        sean 2934 2885 0 21:43:05 pts/1  0:00./die2
                        sean 2935 2934 0    -?             0:00<defunct>
            $ps-ef|grep 2934
            [1]+Exit 199                 /die2
 

Program : 7


       #include<unistd.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/wait.h>
 
void sighandler(int sig){
            printf(“In signal handler for signal%d/n”,sig);
            /*wait0 is the key to acknowledging the SIGGHLD*/
            wait(0);
}
int main (void) {
            int i;
                        /*Assign a signal handler to SIGCHLD*/
                        sigset(SIGCHLD, & sighandler);
                        if(!fork()) {
                                    /*child*/
                                    _exit(o);
                        }
                        sleep(60);
            }
            $gcc die3.c-o die3
            $./die3&
            [1]3116
            $ In signal handler for signal 18
            ps-ef|grep 3116
                        sean 3116 2885 0 22:37:26pts/1   0:00./die3
 
Program : 8
         main()
            {printf(“Files in Directory are:n”);
excel(“.bin.ls”, “ls”, “-1”,0);
}
 
    8.b.         main()
            {int return_value;
                        printf(“Forking processn”);
            fork();
printf(“The process id is %d and return value is %dn”,                                         
getpid(),return_value);
                        excel(“/bin/ls/”, “ls”, “-1”,0);
                        printf (“ This line is not printedn”);
            }
            The output of this would be:
 
            Forking process
            The process id is 6753 and return value is 0
            The process id is 6754 and return value is 0
            Two lists of files in current directory

Articles