Sie sind auf Seite 1von 11

1. continue.

c
#include<stdio.h>
main()
{
int i =1;
while(1)
printf(" loop executing %d\n", i++);
}

// 2. display.c
// this program displays the content of any text file. Usage:

./display <filename>

#include<stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include<unistd.h>
#include<fcntl.h>
// unistd => header file that deals with permissions to create, append, lseek..etc..
int main( int argc, char *argv[] )
{
int fd;
char ch;
fd = open(argv[1], O_CREAT|O_RDONLY);
if (fd <0)
{ printf("open error \n ");
return 0;
}
while( read(fd, &ch, 1) )

//or... ( ch = getc(stdin )!=1);

{
if(ch == EOF)
break;
else
printf("%c",ch);
}
printf("\n");

close (fd);
return 0;
}

//3. create.c
#include<stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include<unistd.h>
#include<fcntl.h>
// unistd => header file that deals with permissions to create, append, lseek..etc..
int main( int argc, char *argv[] )
{
int fd;
char ch;
fd = open(argv[1], O_CREAT|O_TRUNC|O_RDWR );
if (fd <0)
{ printf("open error \n ");
return 0;
}
while( ( ch = getchar() ) !=EOF )

//or... ( ch = getc(stdin )!=1);

if (write(fd, &ch, 1) != 1)
{
printf(" write error \n ");
return 0;
}
close (fd);
return 0;
}
..

4. fileopen.c
#include<unistd.h>
#include<stdio.h>
#include<sys/types.h>
#include< stdlib.h>
#include<fcntl.h>
main()
{
int count ;
char ch;
fd= open("our data.c", O_RDWR|O_CREATE|O-APPEND );
if (fd<0 )
{printf("fileopenerror\n");
exit(0);}
printf(" I am good \n")
return 0
}

5. copy.c
#include<stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include<unistd.h>
#include<fcntl.h>
// unistd => header file that deals with permissions to create, append, lseek..etc..
int main( int argc, char *argv[] )
{
int fd1,fd2,nc,i=0;
char ch;
fd1 = open(argv[1], O_CREAT|O_RDONLY );
if (fd1 <0)
{ printf("open error src file \n ");
return 0;
}
fd2 = open(argv[2], O_CREAT|O_RDWR|O_TRUNC );
if (fd2 <0)
{ printf("open error tgt file \n ");
return 0;
}

nc = lseek(fd1,0,SEEK_END);
lseek(fd1,0,SEEK_SET);
while(i<0)
{
read(fd1,&ch,1);
//printf("%c",ch);
write(fd2,&ch,1);
i++;
}

close(fd1);
close(fd2);
return 0;
}
..

6. append.c
#include<stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include<unistd.h>
#include<fcntl.h>
// unistd => header file that deals with permissions to create, append, lseek..etc..

int main( int argc, char *argv[] )


{
int fd1, fd2, nc, i=0;
// fd1 and fd2 are the file descriptors for the data files [ data1 & data2 ]
char ch;
//...................................................................
fd1 = open(argv[1], O_CREAT|O_WRONLY|O_APPEND );
if (fd1 <0)
{ printf("open error file1 \n ");
return 0;
}
fd2 = open(argv[2], O_CREAT|O_RDONLY );
if (fd2 <0)
{ printf("open error file2 \n ");
return 0;
}
//...........................................................................
nc = lseek(fd2,0,SEEK_END); // to count the number of characters in fd2
//printf("%d",nc);
lseek(fd2,0,SEEK_SET); // resetting back to the start of the data
//......................................................................
while(i<nc)
{
read(fd2,&ch,1); // reading from data2
//printf("%c",ch);
write(fd1,&ch,1);
// writing( appending) to data2, while i < nc
i++;
}

close(fd1);
close(fd2);
return 0;
}

7. lseekdemo.c
#include<stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include<unistd.h>
#include<fcntl.h>
// unistd => header file that deals with permissions to create, append, lseek..etc..
int main( int argc, char *argv[] )
{
int fd1,nc,i=0;
char ch;
//...................................................................
fd1 = open(argv[1], O_WRONLY );
if (fd1 <0)
{ printf("open error lseekfile \n ");
return 0;
}
//...........................................................................
nc = lseek(fd1,20,SEEK_CUR); // to count the number of characters in fd1
printf("%d",nc);
//lseek(fd1,0,SEEK_SET); // resetting back to the start of the data
//......................................................................
//while(i<nc)
//{
//read(fd1,&ch,1); // reading from lseekfile
//write(fd1,&ch,1);

// writing( appending) to data2, while i < nc

//i++;

close(fd1);

return 0;

}
..

8. forkdemo.c
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(void)
{
pid_t pid;
int rv;
switch(pid = fork()) {
case -1:
perror("fork"); /* something went wrong */
exit(1); /* parent exits */
case 0:
printf(" CHILD: This is the child process!\n");
printf(" CHILD: My PID is %d\n", getpid());
printf(" CHILD: My parent's PID is %d\n", getppid());
printf(" CHILD: Enter my exit status (make it small): ");
scanf(" %d", &rv);
printf(" CHILD: I'm outta here!\n");
exit(rv);
default:
printf("PARENT: This is the parent process!\n");
printf("PARENT: My PID is %d\n", getpid());
printf("PARENT: My child's PID is %d\n", pid);
printf("PARENT: I'm now waiting for my child to exit()...\n");
wait(&rv);
printf("PARENT: My child's exit status is: %d\n",
WEXITSTATUS(rv));
printf("PARENT: I'm outta here!\n");
}
return 0;
}
..

9. pipes.c
#include<stdio.h>

#include<stdlib.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<unistd.h>
#include<string.h>
#include<fcntl.h>
typedef struct student
{
int rno;
char name[10];
int marks;
}STUDENT; // type definition which describes the structure of data.
int main()
{
int pfds[2],fd,rv=0; // pipe will have two ends, where the data enters from one end
//( fd[1] , and collected from the other end (fd[0]] -- hence two values pfd[2]
// rv = return value
STUDENT std,s[10]; // instance of object from ( the structure of )student class.
// we expect ten objects ( ten student's data ).
char ch;
int i=0, nr, sum=0, avg, max, min, fsize;
if(pipe(pfds) == -1) // routine care taken, to look into the occurrence of errors.
{
printf("pipe error \n");
return 0;
}
do {
printf("Roll No : ");
scanf("%d",&std.rno);
printf("Name : ");
scanf("%s",std.name);
printf("Marks : ");
scanf("%d",&std.marks);
write(pfds[1],(STUDENT*)&std,sizeof(STUDENT)); // &std is the address of the object
//
write into file
// till now, the parent process was active.
if(!fork()) // to create a child process, object( student's data ) should be
// present in the pipe.
{
printf("entered into child\n");
printf("child process id : %d\n",getpid());
read(pfds[0],(STUDENT*)&std,sizeof(STUDENT));
fd = open("mydata",O_CREAT|O_WRONLY|O_APPEND);
write(fd,(STUDENT*)&std,sizeof(STUDENT));
close(fd); // data file closed.
printf("sent data to file\n");
printf("exit child\n");
exit(0);
}
wait(&rv); //

to

printf("Do u want to continue(press y/n): ");


__fpurge(stdin); // clearing the buffer; double underscorefollowed by fpurge( system macro )
ch = getchar();
}while(ch == 'Y' ||ch == 'y');
fd = open("mydata",O_CREAT|O_RDONLY);
fsize = lseek(fd,0,SEEK_END);
nr = fsize/sizeof(STUDENT);
lseek(fd,0, SEEK_SET);
for(i=0;i<nr;i++)
{

read(fd,(STUDENT*)&s[i],sizeof(STUDENT));
// s[i] array of objects in temp file
printf("%d\t%s\t%d\n",s[i].rno,s[i].name,s[i].marks);

}
close(fd);
//............... fo far, data is stored in a temp buffer. Now we should retrive the data record by record
nr = i;
system("clear");
printf("Roll No\tName\tMarks\n");
for(i=0;i<nr;i++)
{

//

printf("%d\t%s\t%d\n",s[i].rno,s[i].name,s[i].marks);
}
printf("no. of students : %d\n",nr);
// computations
max = s[0].marks;
min = s[0].marks;
for(i=0;i<nr;i++) // linear search follows for max and min marks.
{
if(max < s[i].marks)
max = s[i].marks;
if(min > s[i].marks)
min = s[i].marks;
sum = sum + s[i].marks;
}
avg = sum/nr;
// Results
printf("No. of Students : %d\n",nr);
printf("Max. marks : %d\n",max);
printf("min. marks : %d\n",min);
printf("Average : %d\n",avg);
return 0;

10. sendmessage.c
#include<stdio.h>
#include<stdlib.h>

#include<string.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<unistd.h>
struct msgbuf
{
long int mtype;
char mtext[BUFSIZ];
};

int main()
{
int running = 1;
int msgid;
struct msgbuf msg;

//msgbuf : structure object

char tempbuf[BUFSIZ];
// creating a message queue
msgid = msgget((key_t)1234, 0666|IPC_CREAT); // key_t : a unique key to create any msg queue
if(msgid == -1)
{
printf("unable to create a queue... \n");
return 0;
}

while(running)
{
printf("enter some text: ");
fgets(tempbuf,BUFSIZ,stdin); // getting data from keyboard to buffer
msg.mtype = 1;
strcpy(msg.mtext, tempbuf); // copying data frm buffer to message_structure
if(msgsnd(msgid, (void *)&msg, BUFSIZ, 0) == -1)
{
printf("message send err\n");
return 0;
}
if(strncmp(msg.mtext, "end", 3) == 0)
running = 0;
}

return 0;
}
.

11. receivemessage.c

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<unistd.h>
struct msgbuf
{
long int mtype;
char mtext[BUFSIZ];
};
int main()
{
int running = 1;
int msgid;
struct msgbuf msg;
long int mrcv = 0;
// creating a message queue
msgid = msgget((key_t)1234, 0666|IPC_CREAT);
if(msgid == -1)
{
printf("unable to create a queue... \n");
return 0;
}
// retrive meessage until an "end" is encountered
while(running)
{
if(msgrcv(msgid, (void *)&msg, BUFSIZ,mrcv, 0) == -1)
{
printf("message recv err\n");
return 0;
}
printf("You Wrote : %s \n",msg.mtext);
if(strncmp(msg.mtext, "end", 3) == 0)
running = 0;
}
// deleting message queue
if(msgctl(msgid, IPC_RMID, 0) == -1)
{
printf("msgctl err ...\n");
return 0;
}
return 0;
}

Das könnte Ihnen auch gefallen