Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How can we read datatype char* from the user in a pipe between two processes?
Here is my code:
#include<stdio.h>
#include<unistd.h>
#define MSGSIZE 120
int main (
{
char *msg ;
char inbuff[MSGSIZE];
int p[2];
int ret;
pipe(p);
ret = fork();
if(ret != 0){
close(p[0]);
printf("Please Enter the msg\n");
scanf("%s",msg);
printf("I am the parent sending a msg %s\n",msg);
write(p[1],msg,MSGSIZE);
}
else
{
sleep(10);
close(p[1]);
read(p[0],inbuff,MSGSIZE);
printf("\nI am the child with msg %s",inbuff);
}
return 0;
}
My problem is that whenever the compiler gets into scanf() I am getting an error segmentation fault
You need to define msg as follow:
char msg[MSGSIZE];
You are calling scanf(). You want to tell it to put the entered data somewhere. But you don't give it such a space.
Instead, you give it a pointer, which is uninitialized and therefore points to an unknown location. That leads to segfault.
You could use the inbuf instead.
But you should refrain from using scanf() in this way. If the user enters more data than your buffer can hold, weird things happen.
Instead, use fscanf().
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Ok, so i've been having a probelm with using int and char for my function. And is getting an error message enter image description here want to know how i should fix this with the code that i have:
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp;
char name[100];
int roll_no, chars;
float marks;
fp = fopen("records.txt", "r");
if(fp == NULL)
{
printf("Error opening file\n");
exit(1);
}
printf("Testing fscanf() function: \n\n");
printf("Name:\t\tRoll\t\tMarks\n");
while( fscanf(fp, "Name: %s\t\tRoll no: %d\t\tMarks: %f\n"
, name, &roll_no, &marks) != EOF )
{
printf("%s\t\t%d\t\t%.2f\n", name, roll_no ,marks);
}
fclose(fp);
return 0;
}
The intedend output was this... enter image description here
Advice/help on how to use the char function at line 8 would be appericated
NOTE: This answer refers to revision 1 of the question. Meanwhile, OP has clarified the question and is asking for additional help concerning a related issue. That is why this answer does not fully address the question in its current state.
That's not an error message, that is simply a warning because you declare the variable 'chars' on line 8 and never use it in your program. Your program should be able to run even if there are compiler warnings, which are different from compiler errors.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have a pretty simple program:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
// argc is amount of elements that the user inputs, check if 1234 isn't in code
if (strcmp(argv[argc-1],"1234" != 0)) {
exit(-1);
}
for (int i = 0; i < argc; i++) {
if (strcmp(argv[i],"-h")) {
// do whatever
printf("Hello world!");
} else if (strcmp(argv[i],"-f")) {
// argv[i+1] will be the file, print out the file to console
}
}
}
It allows the user to enter something, say ./my_kill -h 1234. If 1234 isn't the last thing, it's supposed to just exit. Then in the for loop, if -h is used it prints hello world. For some reason it is giving me a segmentation fault and I don't understand why.
You have a parenthesis in the wrong position.
if (strcmp(argv[argc-1],"1234" != 0))
should be
if (strcmp(argv[argc-1],"1234") != 0)
Also, the first thing you should do is check if there is any information inside of argv.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am trying to read from a fifo and i am not understanding it's behaviour.
This is the write side, write.c:
for(int i = 1;i<argc;i++){
if(write(fifoFd,argv[i],strlen(argv[i])) <= 0)
perror("Error writing");
}
And this is the read side, read.c:
char buf[1024];
while(1){
int b = read(fifoFd,buf,1024);
if(b<=0) break;
}
printf("%s\n",buf);
First i start read.c ./read then i execute ./write
If i execute write like this ./write backup *.txt sometimes i get what i expected, i.e, backupexample1.txtexample2.txt.
But sometimes i only get example1.txtexample2.txt and i am not understanding this, where is "backup"?
Your code:
while(1){
int b = read(fifoFd,buf,1024);
if(b<=0) break;
}
printf("%s\n",buf);
You loop, each time through the loop you overwrite the buffer, and then you print the buffer. So, sometimes, you read "backup" followed by "example1.txtexample2.txt" (which overwrites "backup"), other times you read the whole lot at once in a single read.
If you change the loop to read into the unpopulated portion of the buffer, it will behave consistently:
int read = 0;
while(read != 1024){
int b = read(fifoFd,buf+read,1024-read);
if(b<=0) break;
read += b;
}
printf("%s\n",buf);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have written the following code in c, the parent read what he get from child using pipe, and write it to a new file, and the child should wait between each 50 characters he read but the problem is that parent only write one line to the file, I know there is something missing but unable to recognize it?
if(fork())
{
//Parent
read(fd[0],str,sizeof(str));
fprintf(fpnew,str,"w+");
}
else
{
//Child 1
while(fgets(str,n,fp)!=NULL)
{
write(fd[1],str,sizeof(str));
sleep(1+rand()%10);
}
}
You need to put a loop in the parent to read the child output until it is finished. fprintf is the wrong thing to use here. If you want to stick to the std C lib then you probably want fwrite here. But rather than mix and match the stdlib functions with the lower level read I tend to like to be consistent. It's just one less thing to think about. You want something more like this. (Also, read up on the parameters of the functions you are using and check return code.)
int fpnew = open("whatever", O_WRONLY);
if (pfnew < 0)
error....
int bytesread;
while ((bytesread = read(fd[0], str, sizeof(str))) != 0)
write(fpnew, str, bytesread);
close(pfnew);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm taking an Operating Systems class for university and we have an assignment as follows:
Write a program that can be used to create a child process.
The child process should create a file called “Listx.txt” and ask the user for data to write to it. The parent process should read the data from the file and display it on the screen.
Modify the program to make the parent read the file and display the contents five times. It should pause for 1 second between each display.
Modify the program to make the parent read the file and display the contents over and over again until the user sends SIGSTOP. It should pause for 1 second between each display.
And this is the code I've come up with:
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
int main()
{
int x;
int y = 0;
pid_t pid = fork();
if (pid==0)
{
printf("Hi, i am the child\n");
int fd;
fd = open("listx.txt", O_RDWR |O_CREAT |O_TRUNC);
printf ("enter Number");
scanf("%d\n",x);
char wd [100];
ssize_t nr;
wd[0]=x;
nr = write (fd, wd, sizeof (wd));
}
else
printf(" I am the parent, the child is %d\n",pid);
{
int fd;
fd = open ("listx.txt", O_RDONLY);
if (fd == -1)
{
printf("file not opened \n");
}
else
{
printf("file found \n");
}
char wd[100];
ssize_t nr;
nr = read (fd, wd, sizeof (wd));
if (nr == -1)
{
printf("file not read \n");
}
else
{
while (y < 5){
printf("The file has %s \n",wd);
sleep(1);
}
}
return 0;
The program compiles (through GCC) but I think I have the logic wrong.
May you kindly assist with helping me solve this?
This:
scanf("%d\n",x);
char wd [100];
ssize_t nr;
wd[0]=x;
is rather wrong, in more ways than one:
You must pas &x to scanf(), since it can't store the value unless given an address. Instead you pass the current value of x, causing undefined behavior.
You assign the value of x into a single character, which is going to drop lots of bits. This is probably not what you want to do.
You use file descriptors even after detecting that they are not valid.
Please figure out how to maximize the diagnostics (warnings and errors) from your compiler, and observe what it says. Many of these problems will generate warnings. For GCC, this manual page is informative. Basically, start out by adding -Wall -Wpedantic -Wextra to your compiler invocation.
exit the child (_exit(0))
wait in the parent until child has finished (waitpid(2) et.al.)
Apart from scanf problems I see printf(" I am the parent, the child is %d\n",pid); that I suspect that you wanted inside the curly brackets.
Moreover you need to ensure that the child wrote before starting reading so the first instruction in the parent should be waitpid(pid,&status,0); that waits for the child termination (and indirectly for the file being written). Note that the fact that the code of the child is on top doesn't mean that it will executed as first (I think this is what the exercise wants to highlight).
Another thing that you should always do as a good programmer is closing your file after writing.