IPC SYSTEM CALLS PROGRAMS-1
IPC SYSTEM CALLS PROGRAMS-1
SHARED MEMORY:
Client:
//SHMClient.C
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 27
int main()
{
int shmid;
key_t key;
char *shm, *s;
key = 5678;
/*
*Change the first character of the
*segment to '*', indicating we have read
*the segment.
*/
*shm = '*';
puts("\nClient exiting\n");
exit(0);
}
Server:
//SHMServer.C
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define MAXSIZE 27
int main()
{
char c;
int shmid;
key_t key;
char *shm, *s;
key = 5678;
/*
* * Put text into the memory for the
* other process to read.
* */
s = shm;
/*
* Wait until the other process
* changes the first character of our memory
* to '*', indicating that it has read what
* we put there.
*/
while (*shm != '*'){puts("\nServer waiting\n");
sleep(1);}
puts("\nServer exiting after client read data\n");
exit(0);
}
2. Unnamed pipe
// C program to illustrate
// pipe system call in C
// shared by Parent and Child
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <wait.h>
#define MSGSIZE 16
char* msg1 = "hello, world #1";
char* msg2 = "hello, world #2";
char* msg3 = "hello, world #3";
int main()
{
char inbuf[MSGSIZE];
int p[2], pid, nbytes;
if (pipe(p) < 0)
exit(1);
/* continued */
pid = fork();
if (pid> 0) {
write(p[1], msg1, MSGSIZE);
write(p[1], msg2, MSGSIZE);
write(p[1], msg3, MSGSIZE);
else {
// Adding this line will
// not hang the program
// close(p[1]);
while ((nbytes = read(p[0], inbuf, MSGSIZE)) > 0)
printf("%s\n", inbuf);
if (nbytes != 0)
exit(2);
close(p[0]);
printf("Finished reading\n");
printf("Child exitingi\n");
}
return 0;
}
3.Named pipe:
Writer:
// C program to implement one side of FIFO
// This side writes first, then reads
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int fd;
int main()
{
int fd1;
4. Message queues:
sender:
//IPC_msgq_send.c
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXSIZE 128
void main()
{
int msqid;
int msgflg = IPC_CREAT | 0666;
key_t key;
msgbuf sbuf;
size_t buflen;
key = 1234;
if ((msqid = msgget(key, msgflg )) < 0) //Get the message queue ID for the given key
die("msgget");
//Message Type
sbuf.mtype = 1;
buflen = strlen(sbuf.mtext) + 1 ;
else
printf("Message Sent\n");
exit(0);
}
receiver:
//IPC_msgq_rcv.c
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 128
key = 1234;
printf("%s\n", rcvbuffer.mtext);
exit(0);
}
The opendir function opens a directory. It returns a valid pointer if the opening was successful
and NULL otherwise.
The readdir function, at every call, reads another directory entry from the current directory. The
first readdir will read the first directory entry; the second call will read the next entry and so on.
In case of a successful reading the function will return a valid pointer to a structure of
type dirent and NULL otherwise (in case it reached the end of the directory, for example).
The rewinddir function repositions the file pointer to the first directory entry (the beginning of
the directory).
The closedir function closes a previously opened directory. In case of an error it returns the value
-1.
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
void listDir(char *dirName)
DIR* dir;
char name[1000];
dir = opendir(dirName);
if (dir == 0) {
perror ("Error");
exit(1);
while ((dirEntry=readdir(dir)) != 0) {
sprintf(name,"%s/%s",dirName,dirEntry->d_name);
if (S_ISDIR(inode.st_mode))
printf("dir ");
else if (S_ISREG(inode.st_mode))
else
if (S_ISLNK(inode.st_mode))
else;
}
int main(int argc, char **argv)
if (argc != 2) {
exit(0);
listDir(argv[1]);
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <string.h>
if (c == 2) {
myDirectory = opendir(v[1]);
if (myDirectory) {
puts("OK the directory is opened, let's see its files:");
while ((myFile = readdir(myDirectory)))
printf("%s\n", myFile->d_name);
/*
** closedir
*/
if (closedir(myDirectory) == 0)
puts("The directory is now closed.");
else
puts("The directory can not be closed.");
} else if (errno == ENOENT)
puts("This directory does not exist.");
else if (errno == ENOTDIR)
puts("This file is not a directory.");
else if (errno == EACCES)
puts("You do not have the right to open this folder.");
else
puts("That's a new error, check the manual.");
} else
puts("Sorry we need exactly 2 arguments.");
return (0);
}
$ gcc main.c
$ ./a.out hello
All files in the hello directory appear and we finish by closing the directory stream with the
closedir() system call function.