Wednesday, January 19, 2011

a pgm involving shared memory plus file handling!

//parent stores names of 2 files and child finds which one has greater size
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MUTEX 0
///////////////////
void up(int sem_id,int sem_num,struct sembuf* semaphore)
{ semaphore->sem_num=sem_num;
semaphore->sem_op=1;
semaphore->sem_flg=0;
semop(sem_id,semaphore,1);
}
////////////////////
void down(int sem_id,int sem_num,struct sembuf* semaphore)
{ semaphore->sem_num=sem_num;
semaphore->sem_op=-1;
semaphore->sem_flg=0;
semop(sem_id,semaphore,1);
}
//////////////////////////////
void initsem(int sem_id,int sem_num,int val)
{ union semum
{ int val;
struct semid_ds* buf;
unsigned short* array;
}argument;
argument.val=val;
semctl(sem_id,sem_num,SETVAL,argument);
}
////////////////////////////////
void main()
{ pid_t childpid;
int i=0,k=0,fd1,fd2;
int size=20,size1=0,size2=0;
struct sembuf semaphore;
key_t shm_key=1234,sem_key=4567;char * shm,*iterator;
int shm_id,sem_id;char temp[10];char ch;
shm_id=shmget(shm_key,size,IPC_CREAT|0666);
shm=shmat(shm_id,NULL,0);
sem_id=semget(sem_key,1,IPC_CREAT|0666);
initsem(sem_id,MUTEX,1);
childpid=fork();
if (childpid==0)
{ //child
sleep(5);
down(sem_id,MUTEX,&semaphore);
printf("\n child process reads file names\n ");
for(i=0;i<2;i++)
{ printf("\n%s\n",shm+10*i);
}

strcpy(temp,shm);
fd1=open(temp,O_RDONLY);
if(fd1==-1)
printf("\n unable to open file %s",temp);
else
{ size1=0;
while(read(fd1,&ch,sizeof(ch)))
{ size1++;
}
}
strcpy(temp,shm+10);
fd2=open(temp,O_RDONLY);
if(fd2==-1)
printf("\n unable to open file %s",temp);
else
{ size2=0;
while(read(fd2,&ch,sizeof(ch)))
{ size2++;
}
}
if(size1>size2)
printf("%s has more memory:%d",shm,size1);
else
printf("%s has more memory:%d",shm+10,size2);

up(sem_id,MUTEX,&semaphore);
}
else
{//parent
printf("\n parent process takes and sends no.s");

down(sem_id,MUTEX,&semaphore);
k=0;iterator=shm;
while(k<2)
{ printf("\n k=%d,n=%d",k,2);
printf("\nenter string no. %d ",k+1);
scanf("%s",temp);
strcpy(iterator,temp);

k++;iterator+=10;
}
up(sem_id,MUTEX,&semaphore);
waitpid(childpid,NULL,0);
}
}//end of main

IPC using shared memory-11

//parent reads n strings in shared memory and child finds palindromes
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MUTEX 0
///////////////////
void up(int sem_id,int sem_num,struct sembuf* semaphore)
{ semaphore->sem_num=sem_num;
semaphore->sem_op=1;
semaphore->sem_flg=0;
semop(sem_id,semaphore,1);
}
////////////////////
void down(int sem_id,int sem_num,struct sembuf* semaphore)
{ semaphore->sem_num=sem_num;
semaphore->sem_op=-1;
semaphore->sem_flg=0;
semop(sem_id,semaphore,1);
}
//////////////////////////////
void initsem(int sem_id,int sem_num,int val)
{ union semum
{ int val;
struct semid_ds* buf;
unsigned short* array;
}argument;
argument.val=val;
semctl(sem_id,sem_num,SETVAL,argument);
}
////////////////////////////////
void main()
{ pid_t childpid;
int i=0,k=0,n;
printf("\n enter the no of strings(<=10)");
scanf("%d",&n);
int size=n*10;
struct sembuf semaphore;
key_t shm_key=1234,sem_key=4567;char * shm,*iterator;
int shm_id,sem_id,j,no,flag=1;char temp[5];
shm_id=shmget(shm_key,size,IPC_CREAT|0666);
shm=shmat(shm_id,NULL,0);
//shm=shmo;
sem_id=semget(sem_key,1,IPC_CREAT|0666);
initsem(sem_id,MUTEX,1);
childpid=fork();
if (childpid==0)
{ //child
sleep(5);//shm=shmo;
down(sem_id,MUTEX,&semaphore);
printf("\n child process prints sorted strings\n ");
for(i=0;i { printf("\n%s\n",shm+5*i);
}
printf("\n palindromes are");
for(i=0;i { flag=1;
strcpy(temp,shm+5*i);
no=strlen(temp);
for(j=0;j { if (temp[j]!=temp[no-1-j])
{ flag=0;

}
}
if(flag)
printf("\n %s\n",temp);
}
up(sem_id,MUTEX,&semaphore);
}
else
{//parent
printf("\n parent process takes and sends no.s");

down(sem_id,MUTEX,&semaphore);
k=0;iterator=shm;
while(k { printf("\n k=%d,n=%d",k,n);
printf("\nenter string no. %d ",k+1);
scanf("%s",temp);
strcpy(iterator,temp);

k++;iterator+=5;
}
up(sem_id,MUTEX,&semaphore);
waitpid(childpid,NULL,0);
}
}//end of main

IPC using shared memory-1

//parent stores n no.s in shared memory and child finds max n min
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MUTEX 0
///////////////////
void up(int sem_id,int sem_num,struct sembuf* semaphore)
{ semaphore->sem_num=sem_num;
semaphore->sem_op=1;
semaphore->sem_flg=0;
semop(sem_id,semaphore,1);
}
////////////////////
void down(int sem_id,int sem_num,struct sembuf* semaphore)
{ semaphore->sem_num=sem_num;
semaphore->sem_op=-1;
semaphore->sem_flg=0;
semop(sem_id,semaphore,1);
}
//////////////////////////////
void initsem(int sem_id,int sem_num,int val)
{ union semum
{ int val;
struct semid_ds* buf;
unsigned short* array;
}argument;
argument.val=val;
semctl(sem_id,sem_num,SETVAL,argument);
}
////////////////////////////////
void main()
{ pid_t childpid;
int i=0,k=0,n;
printf("\n enter the no of nos(<=10)");
scanf("%d",&n);
int size=n*sizeof(int);
struct sembuf semaphore;
key_t shm_key=1234,sem_key=4567;int * shm;
int shm_id,sem_id,j,temp;int big,small;
shm_id=shmget(shm_key,size,IPC_CREAT|0666);
shm=shmat(shm_id,NULL,0);
sem_id=semget(sem_key,1,IPC_CREAT|0666);
initsem(sem_id,MUTEX,1);
childpid=fork();
if (childpid==0)
{ //child
sleep(1);
down(sem_id,MUTEX,&semaphore);
printf("\n child process prints \n ");
big=shm[0];
for(i=0;i { if(shm[i]>big)
big=shm[i];
}
small=shm[0];
for(i=0;i { if(shm[i] small=shm[i];
}
for(i=0;i { printf("\n%d\n",shm[i]);
}
printf("\nbiggest:%d smallest:%d\n",big,small);
up(sem_id,MUTEX,&semaphore);
}
else
{//parent
printf("\n parent process takes and sends no.s");

down(sem_id,MUTEX,&semaphore);
k=0;
while(k { printf("\n k=%d,n=%d",k,n);
printf("\nenter no. %d ",k+1);
scanf("%d",&shm[k]);
k++;
}
up(sem_id,MUTEX,&semaphore);
waitpid(childpid,NULL,0);
}
}//end of main

IPC using pipes[numbers]-1

//parent sends n no.s child finds prime no.s among them and semds back to parent and parent prints
#include
#include
#include
#include
#include
void main()
{ int buffer[10],temp[10];pid_t childpid;
int pd[2],rpd[2],n,no,j,st,i=0,k=0,num;
printf("\n enter the no of no.s(<=10)");
scanf("%d",&n);

st=pipe(pd);
if(st==-1)
{ printf("\n unable to create pipes");
exit(0);
}
st=pipe(rpd);
if(st==-1)
{ printf("\n unable to create pipes");
exit(0);
}
childpid=fork();
if (childpid==0)
{ //child
sleep(n*3);
printf("\n child process computes\n ");
close(rpd[0]);
close(pd[1]);
read(pd[0],temp,sizeof(temp));
for(i=0;i { num=temp[i];
for(j=2;j { if ((num%j)==0)
{temp[i]=-2;break;}

}
//printf("%d",temp[i]);
}
write(rpd[1],temp,sizeof(temp));
}
else
{//parent
printf("\n parent process takes and sends nos");
close(rpd[1]);
close(pd[0]);
k=0;
while(k { printf("\n k=%d,n=%d",k,n);
printf("\nenter no. %d ",k+1);
scanf("%d",&buffer[k]);
k++;
}
write(pd[1],buffer,sizeof(buffer));
waitpid(childpid,NULL,0);
printf("\n back to parent");
read(rpd[0],buffer,sizeof(buffer));
k=0;
while(k { if(buffer[k]!=-2)
printf("\n %d\n ",buffer[k]);

k++;
}
}
}//end of main

IPC using pipes[strings]-11

//pgm to send n strings from parent to child
#include
#include
#include
#include
#include
void main()
{ char buffer[10][5];pid_t childpid;
int pd[2],n,st,i=0,k=0;
printf("\n enter the no of strings(<=10)");
scanf("%d",&n);

st=pipe(pd);
if(st==-1)
{ printf("\n unable to create pipes");
exit(0);
}
childpid=fork();
if (childpid==0)
{ //child
printf("\n child process prints string\n ");
// char demo[10][5];
close(pd[1]);
read(pd[0],buffer,sizeof(buffer));
for(i=0;i { strcpy(demo[i],buffer[i]);
}

}
else
{//parent
printf("\n parent process takes and sends string");

close(pd[0]);
k=0;
while(k { printf("\n k=%d,n=%d",k,n);
printf("\nenter string %d ",k+1);
scanf("%s",buffer[k]);
k++;
}
write(pd[1],buffer,sizeof(buffer));
waitpid(childpid,NULL,0);
}
}//end of main

IPC using pipes-111

//1 parent and 2 child parent reads no.s and sends to childs using pipe and 1 child computes sum //of odd no.s and other child computes sum of even no.s and sends the result back to parent and //parent prints the result

#include
#include
#include
#include
#include
void main()
{ int buffer[10],temp1[10],temp2[10];pid_t childpid1,childpid2;
int pd1[2],rpd1[2],pd2[2],rpd2[2],n,no,j,st,i=0,k=0,num,sume=0,sumo=0,rsume=0,rsumo=0,tsum=0;
printf("\n enter the no of no.s(<=10)");
scanf("%d",&n);

st=pipe(pd1);
if(st==-1)
{ printf("\n unable to create pipes");
exit(0);
}
st=pipe(rpd1);
if(st==-1)
{ printf("\n unable to create pipes");
exit(0);
}
st=pipe(pd2);
if(st==-1)
{ printf("\n unable to create pipes");
exit(0);
}
st=pipe(rpd2);
if(st==-1)
{ printf("\n unable to create pipes");
exit(0);
}
childpid1=fork();
if (childpid1==0)
{ //child 1
sleep(n*3);
printf("\n child1 process computes\n ");
close(rpd1[0]);
close(pd1[1]);
read(pd1[0],temp1,sizeof(temp1));
for(i=0;i { if(temp1[i]%2==0)
sume+=temp1[i];
}
write(rpd1[1],&sume,sizeof(sume));
}

else
{
childpid2=fork();
if(childpid2==0)
{//child 2

sleep(n*3);
printf("\n child2 process computes\n ");
close(rpd2[0]);
close(pd2[1]);
read(pd2[0],temp2,sizeof(temp2));
for(i=0;i { if(temp2[i]%2!=0)
sumo+=temp2[i];
}
write(rpd2[1],&sumo,sizeof(sumo));
}

else
{//parent
close(pd1[0]);close(pd2[0]);close(rpd1[1]);close(rpd2[1]);
printf("\n parent process takes and sends nos");
k=0;
while(k { printf("\n k=%d,n=%d",k,n);
printf("\nenter no. %d ",k+1);
scanf("%d",&buffer[k]);
k++;
}
write(pd1[1],buffer,sizeof(buffer));
write(pd2[1],buffer,sizeof(buffer));
waitpid(childpid1,NULL,0);waitpid(childpid2,NULL,0);
printf("\n back to parent");
read(rpd1[0],&rsume,sizeof(int));
read(rpd2[0],&rsumo,sizeof(int));
k=0;
tsum=rsumo+rsume;
while(k {
printf("\n %d\n ",buffer[k]);

k++;
}
printf("\n sum of even no.s is %d",rsume);
printf("\n sum of odd no.s is %d",rsumo);
printf("\n sum of all no.s is %d\n",tsum);
}
}
close(pd1[0]);close(pd2[0]);close(rpd1[1]);close(rpd2[1]);
close(pd1[1]);close(pd2[1]);close(rpd1[0]);close(rpd2[0]);
}//end of main

IPC pgm using shard memory and pipes-1

//in this program a parent process reads no.s in shared memory and its child sorts it and //child's child performs binary search on it
#include"stdio.h"
#include"unistd.h"
#include"sys/types.h"
#include"sys/shm.h"
#include"sys/sem.h"
#include"sys/wait.h"
#include"sys/ipc.h"
#define MUTEX 0
////////////////////////
int bsearch(int key,int s,int l,int* temp)
{ if(s>=l)
return -1;
int mid=(s+l)/2;
if(key==temp[mid])
return mid;

else if(key>temp[mid])
{ s=mid+1;
bsearch(key,s,l,temp);
}
else if(key { l=mid-1;
bsearch(key,s,l,temp);
}
}
/////////////////////
void initsem(int sem_id,int sem_num,int value)
{
union semum
{ int val;
struct semid_ds* buf;
unsigned short * array;
}argument;
argument.val=value;
semctl(sem_id,sem_num,SETVAL,argument);
}
//////////////////////////

void up(int sem_id,int sem_num,struct sembuf * semaphore)
{
semaphore->sem_num=sem_num;
semaphore->sem_op=1;
semaphore->sem_flg=0;
semop(sem_id,semaphore,1);
}
//////////////////////////////////////
void down(int sem_id,int sem_num,struct sembuf * semaphore)
{
semaphore->sem_num=sem_num;
semaphore->sem_op=-1;
semaphore->sem_flg=0;
semop(sem_id,semaphore,1);
}
//////////////////////////////////////

void main()
{ key_t shm_key=1234,sem_key=3456;pid_t child1,child2;
int shm_id,sem_id;int size,i,j,pos,key,n,t;int *shm;int temp[20],temp1[20];
int pd[2],st;
struct sembuf semaphore;
printf("\n enter the no of no.s ");
scanf("%d",&n);
size=n*sizeof(int);
st=pipe(pd);
if(st==-1)
printf("\n unable to create pipe");
shm_id=shmget(shm_key,size,IPC_CREAT|0666);
shm=shmat(shm_id,NULL,0);
sem_id=semget(sem_key,1,IPC_CREAT|0666);
initsem(sem_id,MUTEX,1);
child1=fork();
if(child1==0)
{ //child 1 creates its child
child2=fork();
if(child2==0)
{//child2
sleep(n*6);
close(pd[1]);

printf("\n child 2 reads");
down(sem_id,MUTEX,&semaphore);
read(pd[0],temp1,sizeof(temp1));
up(sem_id,MUTEX,&semaphore);
for(i=0;i { printf("\nch2- %d\n",temp1[i]);
}
printf("\n enter the key to be searched");
scanf("%d",&key);
pos=-1;

pos=bsearch(key,0,n,temp1);
if(pos!=-1)
{ printf("\nkey found at position %d",pos);
}
else
printf("\nkey not found\n");


}//inner if
else
{ //child 1
sleep(n*3);close(pd[0]);
printf("\n child 1 reads");
down(sem_id,MUTEX,&semaphore);
for(i=0;i {
for(j=i+1;j { if(shm[i]>shm[j])
{ t=shm[i];
shm[i]=shm[j];
shm[j]=t;
}
}
}
printf("\n the sorted no.s are\n");
for(i=0;i { temp[i]=shm[i];
printf("\nch1- %d",shm[i]);
}

write(pd[1],temp,sizeof(temp));
up(sem_id,MUTEX,&semaphore);

waitpid(child2,NULL,0);
}//inner else

}//end of if
else
{//parent
down(sem_id,MUTEX,&semaphore);
printf("\n enter no.s\n ");
for(i=0;i { printf("\n no. %d ",i+1);
scanf("%d",&shm[i]);
}
up(sem_id,MUTEX,&semaphore);
waitpid(child1,NULL,0);
}//end of else

}

producer/consumer varient

//in this program producer subtracts 2 and consumer adds 2 to a data in shared memory
#include
#include
#include
#include
#include
#include
#include
#include
#define MUTEX 0
////////////////////

void upsem(int sem_id,int snum,struct sembuf * semaphore)
{ semaphore->sem_num=snum;
semaphore->sem_op=1;
semaphore->sem_flg=0;
semop(sem_id,semaphore,1);
}
///////////////////////////////////
void downsem(int sem_id,int snum,struct sembuf * semaphore)
{ semaphore->sem_num=snum;
semaphore->sem_op=-1;
semaphore->sem_flg=0;
semop(sem_id,semaphore,1);
}
///////////
void initsem(int sem_id,int sem_num,int val)
{
union semum
{ int val;
struct semid_ds * buf;
unsigned short *array;
}argument;
argument.val=val;
semctl(sem_id,sem_num,SETVAL,argument);
}
////////////////////////
void main()
{ key_t shm_key=1234,sem_key=5678;
int * shm;int item;int i;
int shm_id,sem_id;
struct sembuf semaphore;
shm_id=shmget(shm_key,sizeof(int),IPC_CREAT|0666);
shm=shmat(shm_id,NULL,0);
sem_id=semget(sem_key,1,IPC_CREAT|0666);

initsem(sem_id,MUTEX,1);
shm[0]=100;
if(fork()==0)
{


while(1)
{ //consumer
sleep(1);

downsem(sem_id,MUTEX,&semaphore);

shm[0]+=4;
printf("\n consumer adds 4 %d ",shm[0]);
upsem(sem_id,MUTEX,&semaphore);

}
}
else
{
while(1)
{//producer)

sleep(1);

downsem(sem_id,MUTEX,&semaphore);

shm[0]-=2 ;
printf("\n producer substracts 2 %d",shm[0]);
upsem(sem_id,MUTEX,&semaphore);

}
}
}//end of main

CAR SHOWROOM(a varient of producer consumer problem)

// there is a car showroom where there can be maximum of 5 cars,manufacturers produce and //customers consume and after 10 cars have been sold we need to exit from the program
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define size 5
#define EMPTY 0
#define FULL 1
#define MUTEX 2
////////////////////

void upsem(int sem_id,int snum,struct sembuf * semaphore)
{ semaphore->sem_num=snum;
semaphore->sem_op=1;
semaphore->sem_flg=0;
semop(sem_id,semaphore,1);
}
///////////////////////////////////
void downsem(int sem_id,int snum,struct sembuf * semaphore)
{ semaphore->sem_num=snum;
semaphore->sem_op=-1;
semaphore->sem_flg=0;
semop(sem_id,semaphore,1);
}
///////////
void initsem(int sem_id,int sem_num,int val)
{
union semum
{ int val;
struct semid_ds * buf;
unsigned short *array;
}argument;
argument.val=val;
semctl(sem_id,sem_num,SETVAL,argument);
}
////////////////////////
void main()
{ key_t shm_key=1234,sem_key=5678;
int * shm;int item;int i,count=0,flag=1;
int shm_id,sem_id;
struct sembuf semaphore;
shm_id=shmget(shm_key,sizeof(int),IPC_CREAT|0666);
shm=shmat(shm_id,NULL,0);
sem_id=semget(sem_key,3,IPC_CREAT|0666);
initsem(sem_id,FULL,0);
initsem(sem_id,EMPTY,size);
initsem(sem_id,MUTEX,1);
shm[0]=0;
if(fork()==0)
{


while(flag)
{ //consumer
sleep(1);
downsem(sem_id,FULL,&semaphore);
downsem(sem_id,MUTEX,&semaphore);
if(count==10)
{ printf("\n 10 cars purchased");
flag=0; shm[0]=-1;
upsem(sem_id,MUTEX,&semaphore);
upsem(sem_id,EMPTY,&semaphore);

}
else
{
item=shm[1];
for(i=1;i {shm[i]=shm[i+1];}
shm[0]--;

count++;

printf("\n consumer buys car no. %d ",count);
}
upsem(sem_id,MUTEX,&semaphore);
upsem(sem_id,EMPTY,&semaphore);
}
}
else
{
while(flag)
{//producer)

item=random()%10;
sleep(1);
downsem(sem_id,EMPTY,&semaphore);
downsem(sem_id,MUTEX,&semaphore);
if(shm[0]==-1)
{flag=0;}
else
{shm[++shm[0]]=item;

printf("\n producer adds car ");
}
upsem(sem_id,MUTEX,&semaphore);
upsem(sem_id,FULL,&semaphore);
}
}
}//end of main

producer/consumer problem

#include
#include
#include
#include
#include
#include
#include
#include
#define size 10
#define EMPTY 0
#define FULL 1
#define MUTEX 0
////////////////////

void upsem(int sem_id,int snum,struct sembuf * semaphore)
{ semaphore->sem_num=snum;
semaphore->sem_op=1;
semaphore->sem_flg=0;
semop(sem_id,semaphore,1);
}
///////////////////////////////////
void downsem(int sem_id,int snum,struct sembuf * semaphore)
{ semaphore->sem_num=snum;
semaphore->sem_op=-1;
semaphore->sem_flg=0;
semop(sem_id,semaphore,1);
}
///////////
void initsem(int sem_id,int sem_num,int val)
{
union semum
{ int val;
struct semid_ds * buf;
unsigned short *array;
}argument;
argument.val=val;
semctl(sem_id,sem_num,SETVAL,argument);
}
////////////////////////
void main()
{ key_t shm_key=1234,sem_key=5678;
int * shm;int item;int i;
int shm_id,sem_id;
struct sembuf semaphore;
printf("\n welcome to pc pgm");
shm_id=shmget(shm_key,size+1,IPC_CREAT|0666);
shm=shmat(shm_id,NULL,0);
sem_id=semget(sem_key,1,IPC_CREAT|0666);
initsem(sem_id,FULL,0);
initsem(sem_id,EMPTY,size);
initsem(sem_id,MUTEX,1);
shm[0]=100;
if(fork()==0)
{


while(1)
{ //consumer
sleep(1);
downsem(sem_id,FULL,&semaphore);
downsem(sem_id,MUTEX,&semaphore);
item=shm[1];
for(i=1;i {shm[i]=shm[i+1];}
shm[0]--;

printf("\n consumer consumes %d ",item);
upsem(sem_id,MUTEX,&semaphore);
upsem(sem_id,EMPTY,&semaphore);
}
}
else
{
while(1)
{//producer)
item=random()%10;
sleep(1);
downsem(sem_id,EMPTY,&semaphore);
downsem(sem_id,MUTEX,&semaphore);
shm[++shm[0]]=item;

printf("\n producer produces %d",item);
upsem(sem_id,MUTEX,&semaphore);
upsem(sem_id,FULL,&semaphore);
}
}
}//end of main

FILE HANDLING

// for simulating dos sopy command,showing file properties and for reversing a file
#include"stdio.h"
#include"unistd.h"
#include"sys/types.h"
#include"fcntl.h"
#include"time.h"
#include"sys/stat.h"
void main()
{ int fd,cho=0;char ch='y';char filename[10];char buffer[10];char c;
int fd2;int offset=-1;
struct stat nfile;
do{
printf("\n press 1 for dos copy command");
printf("\n press 2 for file attributes");
printf("\n press 3 for reverse of file");
scanf("%d",&cho);
printf("\n enter the file name *.*");
scanf("%s",filename);
fd=open(filename,O_RDONLY);
if(fd==-1)
{ printf("\n file cannot be opened \n");
cho=-1;
}
switch(cho)
{ case 1:fd2=creat("copy.c",0);
while(read(fd,&c,sizeof(char)))
{ write(fd2,&c,sizeof(char));
}
printf("\n copy file named copy.c created");

break;
case 2:stat(fd,&nfile);
printf("the user id is %d",nfile.st_uid);
printf("the size is %d",nfile.st_size);
printf("the creation time is %s",ctime(&nfile.st_ctime));
printf("the access time is %s",ctime(&nfile.st_atime));
printf("the modification time is %s",ctime(&nfile.st_mtime));
break;
case 3: fd2=creat("rev.c",0);
while((lseek(fd,offset,SEEK_END)!=-1))
{ read(fd,&ch,sizeof(ch));
write(fd2,&ch,sizeof(ch));
offset--;
}
printf("the reversed file rev.c created!");

break;
default:if(cho!=-1)printf("wrong choice");
}
printf("\n do you want to continue?y/n?");
scanf("%c",&ch);
}while(ch=='y');

}