how to implement pipe function means inter process communication without using pipe() or popen()

My aim is to implement pipe(means inter process communication) without using pipe() or popen()????????????
means communicate two separate any c programme without using pipe.
You have to implement pipe().


I m trying to use shared memory. for this purpose.
Is it ok to use shared memory.?????????????????????/

when i m going to implement through shared memory.
one programme writing in shared memory other programme reading from shared memory.
but it is sequential type.
means first programme write his own whole data in shared memory then next programme starts reading.
first programme
write
write
write
second programme
read
read
read
read
My aim is to implement parallel.
Like if first programme write first data then second programme read it, then first programme write second data then second programme read it........and so on.
1 write
2 read
1 write
2 read
.
.
.
.
and so on

these two program are separate c programme file (two different main() in different file).

In same file then through lock it will work.
But in different file how i implement.
Sequential one i am able to do it.
separate c files for sequential(output w w w... r r r ....) are shown below(after thank you).
But how to implement parallel(output w r w r w r....)???????????????????????????

Thank You

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#define SHMSZ 1

main()
{
int shmid;
key_t key;
char *shm, *s;

key = 5678;

if ((shmid = shmget(key, SHMSZ, 0666)) < 0) {
perror("shmget");
exit(1);
}

if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) {
perror("shmat");
exit(1);
}

for (s = shm; *s != NULL; s++)
{
printf("reading %c\n",*s);
// putchar(*s);
}
putchar('\n');

*shm = '*';
exit(0);
}


#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#define SHMSZ 1

main()
{
char c;
int shmid;
key_t key;
char *shm, *s;

key = 5678;

if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) {
perror("shmget");
exit(1);
}

if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) {
perror("shmat");
exit(1);
}

s = shm;
for (c = 'a'; c <= 'z'; c++)
{
*s++ = c;
printf("writing %c\n",c);
}
*s = NULL;

while (*shm != '*')
sleep(1);
exit(0);
}
You should look at semaphores to coordinate access to the same shared memory location.
Topic archived. No new replies allowed.