Reputation: 871
Where there is for(;;)
I need something to read from stdin and save to N
.
Something with read(stdin,N,sizeof(float))
and while(s!=EOF)
s=getc(stdin)
maybe? I'm assuming a list of numbers is saved on stdin separated end of lines
The question was something like:
Your program must print its pid to standard output. Then it should start to read values of N from standard input until the end of the file. After receiving a signal (SIGUSR1) your program should subtract (then multiply, then divide, then subtract) the value of N from sum (first value of sum=0) as a cycle. After receiving SIGUSR2 your program should print the current sum.
How I interpreted the question and attempt:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
float N,sum=0;
int count=0;
void handler(int s){
count++;
if (s==SIGUSR1){
if (count==1){
sum-=N;
}
if (count==2){
sum*=N;
}
if (count==3){
sum/=N;
}
if (count==4){
sum+=N;
count==0;
}
}
else if (s==SIGUSR2){
printf("sum=%d",sum);
}
}
int main(){
pid_t pid=getpid();
signal(SIGUSR1,handler);
signal(SIGUSR2,handler);
printf("%d",pid);
fflush(stdout);
for(;;)
scanf("%d",&N);
return(0);
}
Upvotes: 0
Views: 1946
Reputation: 361635
Check the return value of scanf
. It returns the number of items successfully matched, so if it doesn't return 1 then you're done.
while (scanf("%d", &N) == 1)
;
Upvotes: 1