0% found this document useful (0 votes)
123 views

File Transfer Using Pipes: Source Code: Server Side

The document describes code for file transfer using pipes in Linux. The server side code opens a FIFO pipe for writing and writes message arguments to it. The client side code opens the pipe for reading and prints received messages. It also includes code for process management using fork(), setting process groups, ignoring child death signals, and waiting on child processes. File management code opens, reads, and writes records to a binary file.

Uploaded by

gopi219
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
123 views

File Transfer Using Pipes: Source Code: Server Side

The document describes code for file transfer using pipes in Linux. The server side code opens a FIFO pipe for writing and writes message arguments to it. The client side code opens the pipe for reading and prints received messages. It also includes code for process management using fork(), setting process groups, ignoring child death signals, and waiting on child processes. File management code opens, reads, and writes records to a binary file.

Uploaded by

gopi219
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

File transfer using pipes: if((read(fd,msgbuf,MSGSIZ+1))>0)

Source Code: printf("message received:%s\n",msgbuf);


Server Side }
#include<stdio.h> }
#include<signal.h>
#include<fcntl.h> Output
#include<errno.h>
#define MSGSIZ 63 [root@localhost filetrans]#cc -o server.out
main(argc,argv) server.c
int argc; [root@localhost filetrans]#./server.out
char *argv[];
{ [root@localhost filetrans]#cc -o client.out
int fd,j,nwrite; client.c
char msgbuf[MSGSIZ]; [root@localhost filetrans]#./client.out meet me
if(argc<2) at 7pm
[root@localhost filetrans]#
{ printf("Usage:<filename><message><messa
ge>...\n"); [root@localhost filetrans]#cc -o server.out
exit(1); server.c
} [root@localhost filetrans]#./server.out
if((fd=open("myfifo",O_WRONLY))<0) message received:meet
perror("fifo opened failed"); message received:me
for(j=1;j<argc;j++) message received:at
{ message received:7pm
strcpy(msgbuf,argv[j]);
Process management system call:
if((nwrite=write(fd,msgbuf,MSGSIZ+1))<=0)
perror("message write failed"); Source Code:
}
exit(0); Fork:
}
#include <sys/types.h>
Client Side #include <unistd.h>
#include<stdio.h> #include <stdio.h>
#include<signal.h>
#include<fcntl.h> int main()
#include<errno.h> {
#define MSGSIZ 63 pid_t pid;
main() char *message;
{ int n;
int fd;
char msgbuf[MSGSIZ+1]; printf("fork program starting\n");
pid = fork();
if(mknod("myfifo",010666,0)<0) switch(pid)
perror("myfifo failed"); {
case -1:
if((fd=open("myfifo",O_RDWR))<0) perror("fork failed");
perror("fifo open failed"); exit(1);
case 0:
for(;;) message = "This is the child";
{ n = 5;
break; pid=4684 pgrp=4693
default: pid=4684 pgrp=4697
message = "This is the parent"; pid=4684 pgrp=4693
n = 3; pid=4684 pgrp=4699
break; pid=4684 pgrp=4693
} pid=4684 pgrp=4701
for(; n > 0; n--) { pid=4684 pgrp=4693
puts(message); pid=4684 pgrp=4703
sleep(1);
} Exit & getpid:
exit(0); #include<signal.h>
} main()
{
Output: int child;
[root@localhost process]$cc f1.c if((child==fork())==0)
[root@localhost process]$./a.out {
fork program starting printf("child pid %d\n",getpid());
This is the child pause();
This is the parent }
This is the parent printf("child pid %d\n",child);
This is the child exit(child);
This is the parent }
This is the child
This is the parent Output:
This is the parent
[root@localhost process]$vi exit.c
Setgrp & Kill: [root@localhost process]$./a.out
#include<signal.h> child pid 4769
main() child pid 4768
{ Wait and Ignoring Death of Child Signal:
register int i;
setpgrp(); #include<signal.h>
for(i=0;i<10;i++) main(argc,argv)
{ int argc;
if(fork()==0) char *argv[];
{ {
if(i&1) int i, ret_val,ret_code;
setpgrp(); if(argc>=1)
printf("pid=%d pgrp= signal(SIGCLD,SIG_IGN);
%d\n",getpid(),getpgrp()); for(i=0;i<15;i++)
pause(); if(fork()==0)
} {
} printf("child process %x\n",getpid());
kill(0,SIGINT); exit(i);
} }
ret_val=wait(&ret_code);
Output: printf("wait ret_val %x ret_code
[root@localhost process]$ cc grp.c %x\n",ret_val,ret_code);
[root@localhost process]$ ./a.out }
pid=4684 pgrp=4693
pid=4684 pgrp=4695 Output:
}
[root@localhost process]$cc wait.c Output:
[root@localhost process]$./a.out
child proc 12b1 [root@localhost process]$cc execute.c
child proc 12b2 [root@localhost process]$./a.out
child proc 12b3 uid 500 euid 500
child proc 12b4 fdmjb -1 fdmaury -1
child proc 12b5 after stuid(500): uid 500 euid 500
child proc 12b6 fdmjb -1 fdmaury -1
child proc 12b7 after setuid(500):uid 500 euid 500
child proc 12b8
child proc 12b9
child proc 12ba File management system calls:
child proc 12bb Source Code:
child proc 12bc
child proc 12bd #include<stdio.h>
child proc 12be #include<fcntl.h>
child proc 12bf
wait ret_val ffffffff ret_code b75dad98 main()
{
Setuid int fp;
char ano,ch;
#include<fcntl.h>
struct emp
main() {
{ char name[40];
int uid,euid,fdmjb,fdmaury; int age;
};
uid=getuid();
euid=geteuid(); struct emp e;
char empname[40];
printf("uid %d euid %d\n",uid,euid); long int recsize;

fdmjb=open("mjb",O_RDONLY); fp=open("emp.txt","O_RDONLY");
fdmaury= open("maury",O_RDONLY);
printf("fdmjb %d fdmaury if(fp==NULL)
%d\n",fdmjb,fdmaury); {
fp=open("emp.txt","O_WRONLY");
setuid(uid); if(fp==NULL)
printf("after setuid(%d):uid d euid {
%d\n",uid,getuid(),geteuid()); printf("cannot open file");
exit(0);
fdmjb = open("mjb",O_RDONLY); }
fdmaury = open("maury",O_RDONLY); }
printf("fdmjb %d fdmaury
%d\n",fdmjb,fdmaury); recsize=sizeof(e);

setuid(euid); while(1)
printf("after setuid(%d):uid %d euid {
%d\n",euid,getuid(),getuid()); printf("\n1.Add Record");
printf("\n2.List Record");
printf("\n3.Modify Record"); close(fp);
printf("n 0.Exit"); exit(0);
printf("\nEnter Choice"); }
scanf("%d",&ch); }
}
switch(ch)
{ [root@localhost filemgmt]$cc prmgt.c
case 1:
ano='y'; [root@localhost filemgmt]$./a.out

while(ano=='y') 1=>Add Record


{ 2=>List Record
printf("\nEnter the name and age:"); 3=>Modify Record
scanf("%s %d",e.name,&e.age); 0=>Exit
write(fp,&e,1);
printf("\n Add no record(y/n)"); Enter Choice:1
scanf("%c",&ano);
} Enter the name and age:Allen 22
break;
Add ano record(y/n):y
case 2: Enter the name and age:Anji 23
while(read(fp,&e,1)==1) Add ano record(y/n):n
printf("\n %s %d",e.name,e.age);
break; 1=>Add Record
2=>List Record
case 3: 3=>Modify Record
ano='y'; 0=>Exit

while(ano=='y') Enter Choice:2


{
printf("\nEnter the name to modify"); Allen 22
scanf("%s",empname); Anji 23

while(read(fp,&e,1)==1) 1=>Add Record


{ 2=>List Record
if(strcmp(e.name,empname)==0) 3=>Modify Record
{ 0=>Exit
printf("\n Enter the new name and age");
scanf("%s %d",e.name,&e.age); Enter Choice:0

write(fp,&e,1);
break; Signals set during process states and
} transitions:
}
Source Code:
printf("\nModify ano record");
scanf("%c",&ano); Signals
}
break; include <stdio.h>
#include <unistd.h>
case 0: #include <fcntl.h>
while(1)
void ouch(int sig) {
{ printf("Hello World!\n");
printf("OUCH! - I got signal %d\n", sleep(1);
sig); }
(void) signal(SIGINT, SIG_DFL); }
}
Output
void abc(int sig)
{ [root@localhost signal]$ cc sig.c
printf("you have pressed ctrl key and the [root@localhost signal]$ ./a.out
signal no is %d",sig); you have pressed ctrl key and the signal no is 3
(void) signal(SIGQUIT,SIG_DFL); Enter the value for i and j10 20
}
Result is 2Hello World!
void illegal(int sig) Hello World!
{ Hello World!
printf("\n you have done illegal Hello World!
instruction"); Hello World!
printf("\n The signal number is Hello World!
%d",sig); Hello World!
exit(0); Hello World!
} Hello World!
Hello World!
void call(int sig) Hello World!
{ OUCH! - I got signal 2
printf("\n Illegal use of system call"); Hello World!
printf("\n The signal number is Hello World!
%d",sig); Hello World!
exit(0); Hello World!
} Hello World!
Hello World!
int main() Quit
{ [root@localhost signal]$
int i,j;
int fd; Alarm:
(void) signal(SIGINT, ouch);
(void) signal(SIGQUIT, abc); #include <signal.h>
(void) signal(SIGILL, illegal); #include <stdio.h>
(void) signal(SIGHUP,SIG_IGN); #include <unistd.h>
(void) signal(SIGSYS,call);
static int alarm_fired = 0;
printf("\nEnter the value for i and j");
scanf("%d %d",&i,&j); void ding(int sig)
{
j=j/i; alarm_fired = 1;
printf("\nResult is %d",j); }

fd=open("file1.c",O_RDONLY); int main()


lseek(fd,0l,5); {
int pid;
printf("alarm application starting\n"); signal(SIGUSR1,def);
if((pid = fork()) == 0) sleep(5);
{ }
sleep(5); }
kill(getppid(), SIGALRM);
exit(0); void abc()
} {
printf("waiting for alarm to go off\n"); sleep(1);
(void) signal(SIGALRM, ding); printf("Bye boys\n");
pause(); exit(0);
}
if (alarm_fired)
printf("Ding!\n"); void def()
{
printf("done\n"); sleep(1);
printf("Hello girls\n");
exit(0); kill(pid,SIGUSR2);
} }

Output: Output:

[root@localhost signal]$cc alarm.c [root@localhost signal]$cc sig2.c


[root@localhost signal]$./a.out [root@localhost signal]$./a.out
alarm application starting Hello boys
waiting for alarm to go off Hello girls
Ding! [root@localhost signal]$Bye boys
done
[root@localhost signal]$ [root@localhost signal]$

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
{

You might also like