How to use time function and pass the time through named pipe - c

I am trying to create client server communication through named pipe. From my client I want to send current time. I am trying to use time() function but the time won't appear on my server side terminal. I just see provided text. What am I doing wrong?
server
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
int main() {
char *pathname = "/tmp/myfifo";
int make_fifo = mkfifo(pathname, 0666);
char str[80];
for(;;){
int opn = open(pathname, O_RDONLY);
read(opn, str, sizeof(str));
close(opn);
}
return 0;
}
client
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <time.h>
int main() {
char *pathname = "/tmp/myfifo";
time_t current_time = time(0);
char str[80];
int fd = open(pathname, O_WRONLY);
fgets(str, 80, stdin);
write(fd, str, sizeof(str));
write(fd, (void*) current_time, sizeof(current_time));
close(fd);
return 0;
}

What am I doing wrong?
client: Not passing the address of current_time.*1
// write(fd, (void*) current_time, sizeof(current_time));
write(fd, &current_time, sizeof(current_time)); // Add &
server: not reading the time.
Ignoring return values of read()/write().
*1 Avoid casting like (void*) current_time. Casting tends to hide errors.

Related

How to send properly data in fifo server

I am trying to create a simple fifo client/server. Whenever I compile the program and try to type some data in client, in server I am getting weird outputs such as:
How can I fix this?
server:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main() {
int fd;
mkfifo("/tmp/my_fifo", 0666);
for(;;){
fd = open("/tmp/my_fifo", O_RDONLY);
int d;
char buf[64];
read(fd, buf, sizeof(buf));
sscanf(buf, "%d", &d);
close(fd);
}
return 0;
}
client:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main(){
int fd;
fd = open("/tmp/my_fifo", O_WRONLY);
int d;
scanf("%d", &d);
char buf[32];
sprintf(buf, "%d", d);
write(fd, buf,strlen(buf));
close(fd);
return 0;
}

Using named pipes in c

i have this simple program that passes a value through a named pipe from child to parent process:
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/stat.h>
#include <semaphore.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <stdio.h>
int main()
{
char * myfifo = "/home/tmp/myfifo";
mkfifo(myfifo, 0666);
int fd,rec;
pid_t c=fork();
if(c==0){
fd = open(myfifo, O_WRONLY);
rec=100;
write(fd, rec, sizeof(rec));
}
if(c>0){
sleep(1);
fd = open(myfifo, O_RDONLY);
read(fd, rec, sizeof(rec));
printf("%d\n",fd);
printf("%d\n",rec);
}
}
This program prints fd=-1 and instead of rec being 100 it prints rec's address.I also tried putting &rec in read and write but it did not solve anything.What am i doing wrong?
There's an issue with this line:
write(fd, rec, sizeof(rec));
This is the prototype of write():
ssize_t write(int fd, const void *buf, size_t count);
That means that you're reading from the memory location stored in rec, not the content of rec.
The same thing applies for read(). You need to pass a pointer to rec instead of rec itself.
Also, always make sure to close files after you open and perform I/O on them.
Here's a correct copy of your code:
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/stat.h>
#include <semaphore.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <stdio.h>
int main()
{
const char *myfifo = "/home/tmp/myfifo";
mkfifo(myfifo, 0666);
int fd, rec;
pid_t c = fork();
if(c == 0) {
fd = open(myfifo, O_WRONLY);
rec = 100;
write(fd, &rec, sizeof(rec));
close(fd);
}
if(c > 0) {
sleep(1);
fd = open(myfifo, O_RDONLY);
read(fd, &rec, sizeof(rec));
printf("%d\n", fd);
printf("%d\n", rec);
close(fd);
}
}
Of course, always make sure you have the proper permissions to create, read, and write files in that directory. Also, make sure the directory /home/tmp exists.

Posix semaphores with FIFO not working properly

i've written a small client-server demo program that should implement a simple comunication between two process using a FIFO and two semaphores.
The problem is that even if i've put a sem_wait before the client reads the fifo, he dosen't wait for the server to write on the fifo so the client reads the old value ("2" instead of "pippo"). I really can't find where the mistake is. Following the code of Server and Client.
I hope someone could help me.
Client:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <signal.h>
#include <stdarg.h>
#include <fcntl.h>
#define FIFO_FILE "MIA_FIFO"
FILE * fiforw ;
int fifo_write ( char * buf );
int fifo_read ( char * buf );
sem_t *pizz;
sem_t *cli;
int main(void){
fiforw=fopen ( FIFO_FILE, "rw");
pizz=sem_open("pizz", O_EXCL, S_IRUSR | S_IWUSR, 0);
cli=sem_open("cli", O_EXCL, S_IRUSR | S_IWUSR, 0);
char buff[20];
int pieces=10;;
sprintf(buff,"%d",pieces);
fifo_write(buff);
sem_post(cli);
sem_wait(pizz);
fifo_read(buff);
printf("I've read %s \n",buff); //here he should read "pippo" instead of "2"
fclose ( fiforw );
}
int fifo_write ( char * buf ) {
fputs ( buf, fiforw );
return 1;
}
int fifo_read ( char * buf ) {
fgets( buf, sizeof(buf)+1, fiforw);
return 1;
}
Server:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <signal.h>
#include <stdarg.h>
#include <fcntl.h>
#define FIFO_FILE "MIA_FIFO"
int fifo_write ( char * buf );
int fifo_read ( char * buf );
sem_t *pizz;
sem_t *cli;
FILE * fiforw;
int main(void){
mkfifo(FIFO_FILE,0666);
fiforw = fopen ( FIFO_FILE, "rw");
char buff[20];
char temp[]="pippo";
pizz = sem_open("pizz", O_CREAT, S_IRUSR | S_IWUSR, 0);
cli= sem_open("cli", O_CREAT, S_IRUSR | S_IWUSR, 0);
//while(1) {
sem_wait(cli);
fifo_read(buff);
printf("the value is %s \n",buff);
sprintf(buff,"%s",temp);
printf("i will write pippo on the fifo \n");
fifo_write(buff);
sem_post(pizz);
fclose ( fiforw );
//}
}
int fifo_write ( char * buf ) {
fputs ( buf, fiforw );
return 1;
}
int fifo_read ( char * buf ) {
fgets( buf, sizeof(buf)+1, fiforw);
return 1;
}
Running server first then client is required. If that is the case, it can be helpful to check return codes from system calls.

C - Scanf not blocking for named pipe / FIFO

Usually when a program call a scanf it waits until something is available in stdin to read from it. I am currently making a fifo for input and another one for output that will be used by another process to write an read from a background proccess. But, the background process seem not to wait for any scanf in it, does anyone know why?
Here is the code:
Background:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
int main()
{
int out, in, err;
char *cFifo = "/tmp/out";
char *cInFifo = "/tmp/in";
mkfifo(cFifo, S_IRUSR|S_IWUSR);
mkfifo(cInFifo, S_IRUSR|S_IWUSR);
out = open(cFifo, O_RDWR|O_TRUNC|O_NONBLOCK);
in = open(cInFifo, O_RDWR|O_TRUNC|O_NONBLOCK);
dup2(out, STDOUT_FILENO);
dup2(out, STDERR_FILENO);
dup2(in, STDIN_FILENO);
scanf("%*c");
while(1)
{
scanf("%*c");
printf("Hello\n");
fflush(stdout);
}
return 0;
}
Foreground:
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
int main()
{
int out, in;
size_t i = 0;
char bufOut[1024];
char *cFifo = "/tmp/out";
char *cFifoIn = "/tmp/in";
out = open(cFifo, O_RDONLY);
in = open(cFifoIn, O_WRONLY);
while(1)
{
i =0;
while(!i)
{
i = read(out, bufOut, 1024);
}
if(i)
write(STDOUT_FILENO, bufOut, i);
}
return 0;
}
I have already tried to force write on the new input fifo but the result is the same.
I already checked for errors, and everything return the expected values, no -1 or any other errors associated with each function

Reading and writng with named pipes C

I'm writing a program that should run indefinitely maintaining the value of a variable. Two other programs could change the value of the variable. I use named pipes to receive and send the variable value to external programs.
Here is my code for the manager of the variable.
manager.c:
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pthread.h>
char a = 'a';
void *editTask(void *dummy)
{
int fd;
char* editor = "editor";
mkfifo(editor, 0666);
while(1)
{
fd = open(editor, O_RDONLY);
read(fd, &a, 1);
close(fd);
}
}
void *readTask(void *dummy)
{
int fd;
char* reader = "reader";
mkfifo(reader, 0666);
while(1)
{
fd = open(reader, O_WRONLY);
write(fd,&a,1);
close(fd);
}
}
int main()
{
pthread_t editor_thread, reader_thread;
pthread_create(&editor_thread, NULL, editTask, NULL);
pthread_create(&reader_thread, NULL, readTask, NULL);
pthread_join (editor_thread, NULL);
pthread_join (reader_thread, NULL);
return 0;
}
This program uses pthreads to separately get external values for the variable and to communicate the current value of the variable to external programs.
The program that is able to write values to the variable is:
writer.c:
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
int main(int argc, char** argv)
{
if(argc != 2)
{
printf("Need an argument!\n");
return 0;
}
int fd;
char * myfifo = "editor";
fd = open(myfifo, O_WRONLY);
write(fd, argv[0], 1);
close(fd);
return 0;
}
The program that could read the current value is:
reader.c:
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
int main()
{
int fd;
char * myfifo = "reader";
fd = open(myfifo, O_RDONLY);
char value = 'z';
read(fd, &value, 1);
printf("The current value of the variable is:%c\n",value);
close(fd);
return 0;
}
I ran these programs in my Ubuntu system as follows:
$ ./manager &
[1] 5226
$ ./writer k
$ ./reader
bash: ./reader: Text file busy
Why doesn't my system allow me to run this program?
Thank you.
You are trying to call both the FIFO and the reader program "reader".
Also, you have no error checking. You have no idea whether those calls to mkfifo and open succeeded or not. Adding this is critical before you attempt to do any troubleshooting.

Resources