IPC : added some bugs
This commit is contained in:
@@ -19,5 +19,17 @@ arguments: arguments.c Makefile
|
||||
no-op: no-op.c Makefile
|
||||
gcc -Wall $< -o $@
|
||||
|
||||
#------------- IPC
|
||||
|
||||
get-signal: get-signal.c Makefile
|
||||
gcc -Wall $< -o $@
|
||||
|
||||
dtime.o: dtime.c my-fifo.h Makefile
|
||||
gcc -Wall -c $<
|
||||
|
||||
fifo-tx: fifo-tx.c my-fifo.h dtime.o Makefile
|
||||
gcc -Wall $< dtime.o -o $@
|
||||
|
||||
fifo-rx: fifo-rx.c my-fifo.h dtime.o Makefile
|
||||
gcc -Wall $< dtime.o -o $@
|
||||
|
||||
|
||||
9
code/dtime.c
Normal file
9
code/dtime.c
Normal file
@@ -0,0 +1,9 @@
|
||||
#include <stddef.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
double dtime(void)
|
||||
{
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, NULL);
|
||||
return (double)tv.tv_sec + ((double)tv.tv_usec)/1e6;
|
||||
}
|
||||
27
code/fifo-rx.c
Normal file
27
code/fifo-rx.c
Normal file
@@ -0,0 +1,27 @@
|
||||
/*** named pipe --- receiver ***/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h> /* for mkfifo */
|
||||
#include <fcntl.h>
|
||||
#include "my-fifo.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int fifo;
|
||||
Message message;
|
||||
double localTS;
|
||||
|
||||
if (argc!=2) exit(1);
|
||||
fifo = mkfifo(argv[1], 0400);
|
||||
if (fifo) {
|
||||
perror("mkfifo fail");
|
||||
exit(2);
|
||||
}
|
||||
read(fifo, &message, sizeof(Message));
|
||||
localTS = dtime();
|
||||
printf("%f %f\n", localTS, message.timestamp);
|
||||
close(fifo);
|
||||
return 0;
|
||||
}
|
||||
24
code/fifo-tx.c
Normal file
24
code/fifo-tx.c
Normal file
@@ -0,0 +1,24 @@
|
||||
/*** named pipe --- transmiter ***/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include "my-fifo.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int fifo;
|
||||
Message message;
|
||||
|
||||
if (argc!=2) exit(1);
|
||||
if (-1==(fifo = open(argv[1], O_WRONLY))) {
|
||||
perror("open fifo for wr");
|
||||
exit(1);
|
||||
}
|
||||
message.pid = getpid();
|
||||
message.timestamp = dtime();
|
||||
write(fifo, &message, sizeof(Message));
|
||||
close(fifo);
|
||||
|
||||
return 0;
|
||||
}
|
||||
7
code/my-fifo.h
Normal file
7
code/my-fifo.h
Normal file
@@ -0,0 +1,7 @@
|
||||
typedef struct {
|
||||
int pid;
|
||||
double timestamp;
|
||||
long notused;
|
||||
} Message;
|
||||
|
||||
double dtime(void);
|
||||
Reference in New Issue
Block a user