Linux Prog

  • July 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Linux Prog as PDF for free.

More details

  • Words: 983
  • Pages: 6
File transfer using pipes: Source Code: Server Side #include<stdio.h> #include<signal.h> #include #include<errno.h> #define MSGSIZ 63 main(argc,argv) int argc; char *argv[]; { int fd,j,nwrite; char msgbuf[MSGSIZ]; if(argc<2) { printf("Usage:<message><mes sage>...\n"); exit(1); } if((fd=open("myfifo",O_WRONLY))<0) perror("fifo opened failed"); for(j=1;j<argc;j++) { strcpy(msgbuf,argv[j]); if((nwrite=write(fd,msgbuf,MSGSIZ+1))<=0 ) perror("message write failed"); } exit(0); } Client Side #include<stdio.h> #include<signal.h> #include #include<errno.h> #define MSGSIZ 63 main() { int fd; char msgbuf[MSGSIZ+1]; if(mknod("myfifo",010666,0)<0) perror("myfifo failed"); if((fd=open("myfifo",O_RDWR))<0) perror("fifo open failed"); for(;;) { if((read(fd,msgbuf,MSGSIZ+1))>0)

printf("message received:%s\n",msgbuf); } } Output [root@localhost filetrans]#cc -o server.out server.c [root@localhost filetrans]#./server.out [root@localhost filetrans]#cc -o client.out client.c [root@localhost filetrans]#./client.out meet me at 7pm [root@localhost filetrans]# [root@localhost filetrans]#cc -o server.out server.c [root@localhost filetrans]#./server.out message received:meet message received:me message received:at message received:7pm Process management system call: Source Code: Fork: #include <sys/types.h> #include #include <stdio.h> int main() { pid_t pid; char *message; int n; printf("fork program starting\n"); pid = fork(); switch(pid) { case -1: perror("fork failed"); exit(1); case 0: message = "This is the child"; n = 5; break;

default: message = "This is the parent"; n = 3; break; } for(; n > 0; n--) { puts(message); sleep(1); } exit(0); } Output: [root@localhost process]$cc f1.c [root@localhost process]$./a.out fork program starting This is the child This is the parent This is the parent This is the child This is the parent This is the child This is the parent This is the parent Setgrp & Kill: #include<signal.h> main() { register int i; setpgrp(); for(i=0;i<10;i++) { if(fork()==0) { if(i&1) setpgrp(); printf("pid=%d pgrp= %d\n",getpid(),getpgrp()); pause(); } } kill(0,SIGINT); } Output: [root@localhost process]$ cc grp.c [root@localhost process]$ ./a.out pid=4684 pgrp=4693 pid=4684 pgrp=4695 pid=4684 pgrp=4693

pid=4684 pgrp=4697 pid=4684 pgrp=4693 pid=4684 pgrp=4699 pid=4684 pgrp=4693 pid=4684 pgrp=4701 pid=4684 pgrp=4693 pid=4684 pgrp=4703 Exit & getpid: #include<signal.h> main() { int child; if((child==fork())==0) { printf("child pid %d\n",getpid()); pause(); } printf("child pid %d\n",child); exit(child); } Output: [root@localhost process]$vi exit.c [root@localhost process]$./a.out child pid 4769 child pid 4768 Wait and Ignoring Death of Child Signal: #include<signal.h> main(argc,argv) int argc; char *argv[]; { int i, ret_val,ret_code; if(argc>=1) signal(SIGCLD,SIG_IGN); for(i=0;i<15;i++) if(fork()==0) { printf("child process %x\n",getpid()); exit(i); } ret_val=wait(&ret_code); printf("wait ret_val %x ret_code %x\n",ret_val,ret_code); } Output:

[root@localhost process]$cc wait.c [root@localhost process]$./a.out child proc 12b1 child proc 12b2 child proc 12b3 child proc 12b4 child proc 12b5 child proc 12b6 child proc 12b7 child proc 12b8 child proc 12b9 child proc 12ba child proc 12bb child proc 12bc child proc 12bd child proc 12be child proc 12bf wait ret_val ffffffff ret_code b75dad98 Setuid

Output: [root@localhost process]$cc execute.c [root@localhost process]$./a.out uid 500 euid 500 fdmjb -1 fdmaury -1 after stuid(500): uid 500 euid 500 fdmjb -1 fdmaury -1 after setuid(500):uid 500 euid 500 File management system calls: Source Code: #include<stdio.h> #include main() { int fp; char ano,ch;

#include main() { int uid,euid,fdmjb,fdmaury; uid=getuid(); euid=geteuid(); printf("uid %d euid %d\n",uid,euid); fdmjb=open("mjb",O_RDONLY); fdmaury= open("maury",O_RDONLY); printf("fdmjb %d fdmaury %d\n",fdmjb,fdmaury); setuid(uid); printf("after setuid(%d):uid d euid %d\n",uid,getuid(),geteuid()); fdmjb = open("mjb",O_RDONLY); fdmaury = open("maury",O_RDONLY); printf("fdmjb %d fdmaury %d\n",fdmjb,fdmaury); setuid(euid); printf("after setuid(%d):uid %d euid %d\n",euid,getuid(),getuid()); }

struct emp { char name[40]; int age; }; struct emp e; char empname[40]; long int recsize; fp=open("emp.txt","O_RDONLY"); if(fp==NULL) { fp=open("emp.txt","O_WRONLY"); if(fp==NULL) { printf("cannot open file"); exit(0); } } recsize=sizeof(e); while(1) { printf("\n1.Add Record"); printf("\n2.List Record"); printf("\n3.Modify Record");

printf("n 0.Exit"); printf("\nEnter Choice"); scanf("%d",&ch); switch(ch) { case 1: ano='y'; while(ano=='y') { printf("\nEnter the name and age:"); scanf("%s %d",e.name,&e.age); write(fp,&e,1); printf("\n Add no record(y/n)"); scanf("%c",&ano); } break; case 2: while(read(fp,&e,1)==1) printf("\n %s %d",e.name,e.age); break; case 3: ano='y'; while(ano=='y') { printf("\nEnter the name to modify"); scanf("%s",empname); while(read(fp,&e,1)==1) { if(strcmp(e.name,empname)==0) { printf("\n Enter the new name and age"); scanf("%s %d",e.name,&e.age); write(fp,&e,1); break; } }

exit(0); } } } [root@localhost filemgmt]$cc prmgt.c [root@localhost filemgmt]$./a.out 1=>Add Record 2=>List Record 3=>Modify Record 0=>Exit Enter Choice:1 Enter the name and age:Allen 22 Add ano record(y/n):y Enter the name and age:Anji 23 Add ano record(y/n):n 1=>Add Record 2=>List Record 3=>Modify Record 0=>Exit Enter Choice:2 Allen 22 Anji 23 1=>Add Record 2=>List Record 3=>Modify Record 0=>Exit Enter Choice:0 Signals set during process states and transitions: Source Code:

printf("\nModify ano record"); scanf("%c",&ano); } break; case 0: close(fp);

Signals include <stdio.h> #include #include

void ouch(int sig) { printf("OUCH! - I got signal %d\n", sig); (void) signal(SIGINT, SIG_DFL); }

{ printf("Hello World!\n"); sleep(1); } } Output

void abc(int sig) { printf("you have pressed ctrl key and the signal no is %d",sig); (void) signal(SIGQUIT,SIG_DFL); } void illegal(int sig) { printf("\n you have done illegal instruction"); printf("\n The signal number is %d",sig); exit(0); } void call(int sig) { printf("\n Illegal use of system call"); printf("\n The signal number is %d",sig); exit(0); } int main() { int i,j; int fd; (void) signal(SIGINT, ouch); (void) signal(SIGQUIT, abc); (void) signal(SIGILL, illegal); (void) signal(SIGHUP,SIG_IGN); (void) signal(SIGSYS,call);

[root@localhost signal]$ cc sig.c [root@localhost signal]$ ./a.out you have pressed ctrl key and the signal no is 3 Enter the value for i and j10 20 Result is 2Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! OUCH! - I got signal 2 Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Quit [root@localhost signal]$ Alarm: #include <signal.h> #include <stdio.h> #include static int alarm_fired = 0;

printf("\nEnter the value for i and j"); scanf("%d %d",&i,&j); j=j/i; printf("\nResult is %d",j); fd=open("file1.c",O_RDONLY); lseek(fd,0l,5); while(1)

void ding(int sig) { alarm_fired = 1; } int main() { int pid; printf("alarm application starting\n");

if((pid = fork()) == 0) { sleep(5); kill(getppid(), SIGALRM); exit(0); } printf("waiting for alarm to go off\n"); (void) signal(SIGALRM, ding); pause(); if (alarm_fired) printf("Ding!\n");

sleep(5); } } void abc() { sleep(1); printf("Bye boys\n"); exit(0); }

}

void def() { sleep(1); printf("Hello girls\n"); kill(pid,SIGUSR2); }

Output:

Output:

[root@localhost signal]$cc alarm.c [root@localhost signal]$./a.out alarm application starting waiting for alarm to go off Ding! done [root@localhost signal]$

[root@localhost signal]$cc sig2.c [root@localhost signal]$./a.out Hello boys Hello girls [root@localhost signal]$Bye boys

printf("done\n"); exit(0);

Signals between processess: #include <stdio.h> #include <signal.h> void abc(); void def(); int pid,i; main() { pid=fork(); if (pid==0) { signal(SIGUSR2,abc); sleep(1); printf("Hello boys\n"); kill(getppid(),SIGUSR1); sleep(5); } else { signal(SIGUSR1,def);

[root@localhost signal]$

Related Documents

Linux Prog
July 2020 10
Prog
December 2019 62
Prog
November 2019 57
Prog
May 2020 35
Prog.2
June 2020 6