Showing posts with label Unix Programing. Show all posts
Showing posts with label Unix Programing. Show all posts

Thursday, 4 December 2014


#include<stdio.h>
#include<sys/resource.h>a
#include"myHeader.h"
main()
{
printf("system=%d parent=%d\n",getpid(),getppid());
printf("p2 id=%d \tp2 parent=%d\n",getpid(),getppid());
system("date");
system("cal");
system("./system");
printf("p2=%d parent=%d\n",getpid(),getppid());
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex44.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
system=6169 parent=2817
p2 id=6169  p2 parent=2817
Fri Apr  4 09:10:48 IST 2014
     April 2014       
Su Mo Tu We Th Fr Sa  
       1  2  3  4  5  
 6  7  8  9 10 11 12  
13 14 15 16 17 18 19  
20 21 22 23 24 25 26  
27 28 29 30           
                      
sh: 1: ./system: not found
p2=6169 parent=2817
Read More

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int child(int a,int b)
{
int sum;
sum=a+b;
sleep(5);
return(sum);
//parent(sum);
}
int parent(int sum)
{
int status;
wait(&status);
status>>=8;
//printf("sum=%d\n",sum);
//printf("sum=%d\n",sum);
}
main(int argc,char **argv)
{
int a,b,sum=0;
if(argc!=3)
printf("enter correct no of arguments\n");
a=atoi(argv[1]);
b=atoi(argv[2]);
if(fork())
printf("sum in parent process is=%d\n",parent(sum));
//printf("sum in parent process=%d\n",parent(sum));
else
printf("sum in child process=%d\n",child(a,b));
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex43.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out 10 20
sum in child process=30
sum in parent process is=24
Read More

#include<stdio.h>
#include<stdlib.h>

void child1(void)
{
int t;
printf("in child1\n");
srand(getpid());
t=rand()%10+1;
printf("child1 delay for %d seconds\n",t);
sleep(t);
printf("child1 exiting....\n");
exit(0);
}
void child2(void)
{
int t;
printf("in child2\n");
srand(getpid());
t=rand()%10+1;
printf("child2 delay for %d seconds\n",t);
sleep(t);
printf("child2 exiting....\n");
exit(0);
}
void parent(void)
{
int status;
printf("parent waiting...\n");
while(1){
wait(&status);
status>>=8;
if(status==2)
break;
}
printf("child exiting...\n");
printf("parent exiting....\n");
exit(0);
}
main()
{
if(fork())
{
 if(fork())
 parent();
 else
 child2();
}
else
child1();
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
in child1
parent waiting...
child1 delay for 2 seconds
in child2
child2 delay for 3 seconds
child1 exiting....
child2 exiting....
Read More

Program for sigalrm in UNIX Operating system


#include<stdio.h>
#include<stdlib.h>
#include<signal.h>
void isr(int n)
{
static int i=0;
printf("in ISR......\n");
if(++i==3)
exit(0);
alarm(2);
}
main()
{
printf("proces:%d\n",getpid());
//signal(SIGINT,SIG_DFL);
alarm(3);
signal(SIGALRM,isr);
printf("alarm is set....\n");
//printf("in infinite loop...\n");

while(1);
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex40.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
proces:5897
alarm is set....
in ISR......
in ISR......
in ISR......
Read More

#include"myHeader.h"
void isr(int n)
{
printf("ALARM.....\n");
alarm(2);
}
main()
{
struct sigaction v;
printf("%d\n",getpid());
alarm(5);
v.sa_handler=isr;
v.sa_flags=SA_RESETHAND;
v.sa_flags=0;
sigemptyset(&v.sa_mask);
sigaction(SIGALRM,&v,NULL);
printf("ALARM is set...\n");
printf("in infinite loop....\n");
while(1);
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
5876
ALARM is set...
in infinite loop....

ALARM.....
ALARM.....
ALARM.....
ALARM.....
ALARM.....
ALARM.....
ALARM.....
ALARM.....
ALARM.....
ALARM.....

ALARM.....
ALARM.....
^C
Read More

program to lock a file in Unix Operating system


#include"myHeader.h"
#include<sys/stat.h>
#include<sys/ipc.h>
main()
{
struct flock lockbuf;
int fd;
fd=open("datafile",O_RDWR);
getchar();
printf("enter to lock file...\n");
lockbuf.l_type=F_WRLCK;
lockbuf.l_whence=0;
lockbuf.l_start=0;
lockbuf.l_len=0;
if(fcntl(fd,F_SETLKW,&lockbuf)<0)
perror("fcntl");
printf("LOCKED");
close(fd);
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out

enter to lock file...
fcntl: Bad file descriptor
LOCKED
Read More

program to lock a file in Unix Operating system


#include"myHeader.h"
#include<sys/stat.h>
#include<sys/ipc.h>
main()
{
struct flock lockbuf;
int fd;
fd=open("datafile",O_RDWR);
getchar();
printf("enter to lock file...\n");
lockbuf.l_type=F_WRLCK;
lockbuf.l_whence=0;
lockbuf.l_start=0;
lockbuf.l_len=0;
if(fcntl(fd,F_SETLKW,&lockbuf)<0)
perror("fcntl");
printf("LOCKED");
close(fd);
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out

enter to lock file...
fcntl: Bad file descriptor
LOCKED
Read More

program to lock a file in Unix Operating system


#include"myHeader.h"
#include<sys/stat.h>
#include<sys/ipc.h>
main()
{
struct flock lockbuf;
int fd;
fd=open("datafile",O_RDWR);
getchar();
printf("enter to lock file...\n");
lockbuf.l_type=F_WRLCK;
lockbuf.l_whence=0;
lockbuf.l_start=0;
lockbuf.l_len=0;
if(fcntl(fd,F_SETLKW,&lockbuf)<0)
perror("fcntl");
printf("LOCKED");
close(fd);
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out

enter to lock file...
fcntl: Bad file descriptor
LOCKED
Read More

program to view semaphore operation in Unix system


#include"myHeader.h"
main(int argc,char **argv)
{
int id;
struct sembuf v;
id=semget(1,3,IPC_CREAT|0600);
printf("id=%d in process %d\n",id,getpid());
printf("about to pend on semaphore\n");
v.sem_num=0;
v.sem_op=0;
v.sem_flg=0;
semop(id,&v,1);
printf("after semop on critical region\n");
printf("process exiting...\n");
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex36.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
id=0 in process 5741
about to pend on semaphore
after semop on critical region
process exiting...
Read More

Program to read a file


#include"myHeader.h"
main(int argc,char **argv)
{
char ch;
int fd;
fd=open(argv[1],O_RDONLY);
if(fd==-1)
{
perror("open");
return;
}
while(read(fd,&ch,1)==1)
{
write(1,&ch,1);
//printf("%c",ch);
}
close(fd);
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out madan
welcome madan reddy
hyderabad
always truth wins
Read More

#include"myHeader.h"
main(int argc,char **argv)
{
int ret,i;
char readbuf[30];
ret=open(argv[1],O_RDONLY);
if(argc!=2)
printf("enter correct arguments\n");
if(ret==-1)
{
perror("open");
return;
}
printf("file description:%d\n",ret);
while((read(ret,readbuf,30)))
printf("%s",readbuf);
//readbuf[20]='\0';
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex34.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out madan
file description:5
welcome madan reddy
hyderabad
always truth wins
y
hyderabad
Read More

program to refer process id's in UNIX


#include"myHeader.h"
main()
{
int i;
printf("process:%d\n",getpid());
for(i=1;i<=3;i++)
{
fork();
printf("process id:%d\n",getpid());
}
printf("exiting ......\n");
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex33.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
process:5492
process id:5492
process id:5492
process id:5494
process id:5492
exiting ......
process id:5494
exiting ......
process id:5495
exiting ......
process id:5496
exiting ......
process id:5493
process id:5493
process id:5493
exiting ......
madan@madan-Lenovo-G570:~/madan/osconcepts$ process id:5497
process id:5498
process id:5497
exiting ......
exiting ......
process id:5499
exiting ......
Read More

Program to view relationship among process id's


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
main()
{
if(fork())
{
//system("date");
        printf("parent=%d bash=%d\n",getpid(),getppid());

        if(fork())
        {
//      system("cal");
        printf("parent=%d bash=%d\n",getpid(),getppid());
        if(fork())
        {
        printf("parent=%d bash=%d\n",getpid(),getppid());
        if(fork())
        printf("parent=%d bash=%d\n",getpid(),getppid());
                else
                printf("in process3 child3=%d parent= %d...\n",getpid(),getppid());
        }
        else
        printf("in process2 child2= %d parent=%d...\n",getpid(),getppid());
        }
else
printf("in process1 child1= %d parent=%d...\n",getpid(),getppid());
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex32.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
parent=5464 bash=2817
parent=5464 bash=2817
in process1 child1= 5466 parent=5464...
parent=5464 bash=2817
parent=5464 bash=2817
in process2 child2= 5467 parent=5464...
in process3 child3=5468 parent= 1144...
Read More

#include"myHeader.h"
main()
{
printf("child:%d parent %d groupid:%d uid:%d\n",getpid(),getppid(),getgid(),getuid());
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex31.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
child:5444 parent 2817 groupid:1000 uid:1000
Read More

Example program on preprocessor directives


#include<stdio.h>
#include<stdlib.h>
#define a 2+5
//#define a 5-2
main()
{
int i;
i=a*a*a;
printf("result:%d\n",i);
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex30.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
result:27
Read More

Program to find size of pipe in UNIX


#include"myHeader.h"
int i=0;
main()
{
int count=0;
int fp[2];
pipe(fp);
while(1)
{
printf("count=%d\n",count);
count++;
write(fp[1],&count,1);
}
}
OutPut:
count=65535
count=65536
Read More

Program to implement fork system call in UNIX


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
main()
{
if(fork())
printf("parent=%d bash=%d\n",getpid(),getppid());
else
if(fork())
printf("child4==%d parent=%d\n",getpid(),getppid());
else
if(fork())
printf("child3=%d parent=%d\n",getpid(),getppid());
else
if(fork())
printf("child2=%d parent=%d\n",getpid(),getppid());
else
printf("child1=%d parent=%d\n",getpid(),getppid());
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex26.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
parent=4954 bash=2817
madan@madan-Lenovo-G570:~/madan/osconcepts$ child4==4955 parent=1144
child3=4956 parent=4955
child2=4957 parent=1144
child1=4958 parent=4957
Read More

#include"myHeader.h"
main()
{
int i,status;
if(fork()>0)
{
printf("parent id:%d\n",getppid());
for(i=6;i<10;i++)
{
printf("i=%d\n",i);
sleep(1);
}
wait(&status);
}
else if(fork()==0)
{
printf("child id:%d\n",getpid());
for(i=10;i<25;i++)
{
printf("i=%d\n",i);
sleep(1);
}
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex25.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
parent id:2817
i=6
child id:4930
i=10
i=7
i=11
i=8
i=12
i=9
i=13
i=14
madan@madan-Lenovo-G570:~/madan/osconcepts$ i=15
i=16
i=17
i=18
i=19
i=20
i=21
i=22
i=23
i=24
Read More

Program to implement sleep function in UNIX


#include"myHeader.h"
int pid[3];
main()
{
int status;
if(pid[2]==fork())
{
printf("in parent2..\n");
wait(-1,&status,NULL);
if(pid[1]==fork())
{
printf("in parent1..\n");
if(pid[0]==fork())
printf("in parent0..\n");
else
{
sleep(1);
printf("in child0 process(1sec)...\n");
}
}
else
{
sleep(10);
printf("in child1 process(10sec)...\n");
}
}
else
{
sleep(5);
printf("in child2 process(5sec)...\n");
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex24.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
in parent2..
in parent1..
in parent0..
in child0 process(1sec)...
in child2 process(5sec)...
madan@madan-Lenovo-G570:~/madan/osconcepts$ in child1 process(10sec)...
Read More

#include"myHeader.h"
int data=10;
main()
{
int status;
if(fork()==0)
{
printf("data in child process:%d\n",data);
sleep(5);
printf("child exiting...\n");
}
else
{
printf("data in parent process:%d\n",data);
wait(&status);
printf("parent exiting....\n");
//sleep(2);
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex23.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
data in parent process:10
data in child process:10
child exiting...
parent exiting....
Read More