I have to write a program about signals and alarms, and i can't use semaphores
A first process (named "B") launches another one (named "A"). "A" have to write random words in a file, once per 5 secs, if it writes the word END terminates the execution. The process "B" have to read the words from the file, if read END then relaunch the process "A". When "B" reads 50 words kills "A" and terminates the execution
The program does not work properly,I think it's because both processes accessing the file at the same time, but I do not know for sure
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#define SECS 5
#define MAX_READ 50
#define MAX_TAM 10
#define N_WORDS 13
int main(void){
int i=0,flag;
pid_t pid;
static char words[][MAX_TAM]={"THE","PROCESS","A","WRITES","IN","A","FILE","WORDS","AND","KILL","ITSELF","WITH","END"};
char *word;
char read[MAX_TAM];
FILE *fp=fopen("file2.txt","w");
if(!fp){
perror("Error opening file");
exit(EXIT_FAILURE);
}
if (signal(SIGALRM, SIG_IGN) == SIG_ERR){
puts("Error");
exit (EXIT_FAILURE);
}
while(1){
flag=0;
switch(pid=fork()){
case -1:
perror("fork error");
exit(EXIT_FAILURE);
case 0: /* A */
srand(getpid());
while(1){
if(alarm(SECS))
pause();
word = words[rand()%N_WORDS];
fprintf(fp,"%s ",word);
printf("A ~ WORD: %s\n",word);
fflush(stdout);
if(!strcmp(word,"END")){
fclose(fp);
printf("ENDING A\n");
exit(EXIT_SUCCESS);
}
}
default: /* Proceso B */
sleep(1);
for( ;i<MAX_READ;i++){
if(alarm(SECS))
pause();
fscanf(fp,"%s",read);
printf("B ~ READING %s\n",read);
fflush(stdout);
if(!strcmp(read,"END")){
flag=1;
break;
}
}
}
if(flag==0)
break;
}
kill(pid,9);
exit(EXIT_SUCCESS);
}
Related
I want to duplicate the message that is being printed in stdout when I hit case 's' to the opened file for case 'f' using dup() and dup2().
I'm not sure how the dup system calls work and how I could dup the stdout to the file. I know I would have to use dup to capture what stdout is pointing at then use dup2 to switch between the screen and the file.
Here is my code:
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#define BUFFER_SIZE 128
#define PERMS 0666
int main(int argc, char *argv[])
{
char outBuffer[BUFFER_SIZE] = "This is a message\n";
int count;
int fd;
char input =0;
int a;
if(argc!=2){
printf("Provide an valid file as an argument\n");
exit(1);
}
if((fd = open(argv[1],O_CREAT|O_WRONLY|O_APPEND,PERMS)) == -1)
{
printf("Could not open file\n");
exit(1);
}
printf("0 to terminate, s to write to stdout, f to write to file\n");
do{
scanf(" %c", &input);
switch(input)
{
case 'f':
case 's':
write(1,outBuffer,strlen(outBuffer));
break;
default:
printf("Invalid Choice\n");
}
}while(input != '0');
close(fd);
return 0;
}
The dup system call duplicates the file descriptor which creates a second handle for the program to write to wherever the first one is connected.
It does not duplicate whatever activity goes on the i/o channel. If you want that, you'll have to do two (or more) writes.
Consider using a the 'tee' program (pipe to a child process), which has the ability to send stdout to a file.
case 'f':
pipe fd2[2] ;
pipe(fd2) ;
if ( fork() > 0 ) {
// Child
dup2(fd[0], 0) ;
close(fd[1]) ;
close(
// create command line for 'tee'
char teecmd[256] ;
sprintf(teecmd, "...", ...) ;
execlp(teecmd) ;
} ;
// Parent
fd = fd[1] ;
close(fd[0]) ;
...
I am trying to make a modified shell.
I have tried several times, I searched a lot on the net, but I cannot fix my problem. first of all I do not want to run this code by pipe or semaphore.
I think with semaphore it would be even more difficult, because I do not now how many process should be there (maybe I am wrong, but it seems at least in this way).
In the first way, program makes the file correctly, but I cannot run a loop until the user enter "exit". I am trying with while(1) but it does not give me more than one file and the stdin is active only once.
in the second way, the user can use stdin until he inserts "exit", but again,
it does not give more than one file, moreover there is nothing inside.
I am trying to mix these two ways, I tried for more than one day, but I do not understand where is the problem. By the way, I think in the second way,
it should be the file descriptor.
also, please let me know that do I use the wait system call correctly or not?
Thank you in advance.
/first way/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <fcntl.h>
//void MYshell(char *comm);
int main(){
/*
char cmd[100];
printf("<exit> closes shell\n");
printf("MYshell:$ ");
fscanf(stdin,"%s",cmd);
do{
if(strcmp(cmd,"exit")==0){
printf("shell has been closed.\n");
break;
exit(EXIT_SUCCESS);
}
MYshell(cmd);
printf("MYshell:$ ");
fscanf(stdin,"%s",cmd);
}while(1);
*/
//void MYshell(char *comm){
pid_t fork_ret;
int fd,dup_ret;
char cmd[100];
char name[100];
printf(" <exit> closes shell\n");
printf("MYshell:$ ");
fscanf(stdin,"%s",cmd);
if(strcmp(cmd,"exit")==0){
printf("shell has been closed.\n");
//break;
exit(EXIT_SUCCESS);
}
fork_ret = fork();
if(fork_ret == -1)
exit(1);
if(fork_ret == 0){
sprintf(name,"%d.log",getpid());
fd = open(name,O_CREAT | O_WRONLY,S_IRWXU|S_IRWXG|S_IRWXO);
if(fd<0){
printf("Error in opening or creating %s.\n",name);
exit(1);
}
dup_ret = dup2(fd,1);
if(dup_ret<0){
printf("Error in duplicating stdout descriptor.\n");
exit(1);
}
system(cmd);
close(fd);
exit(0);
}
//close(fd);
//exit(EXIT_SUCCESS);
if(fork_ret>0){
int stat;
pid_t child = wait(&stat);
printf("child = %d\n",child);
if(WIFEXITED(stat))
printf("ok\n");
else
printf("error\n");
}
wait(NULL);
exit(EXIT_SUCCESS);
}
/* second way*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <fcntl.h>
int fd;
char cmd[100];
void MYshell(void);
int main(){
pid_t fork_ret;
//int fd,dup_ret;
printf(" <exit> closes shell\n");
while(1){
printf("MYshell:$ ");
fscanf(stdin,"%s",cmd);
//tmp = fgets(cmd,sizeof(cmd),stdin);
if(strcmp(cmd,"exit")==0){
printf("shell has been closed.\n");
break;
exit(EXIT_SUCCESS);
}
}
fork_ret = fork();
if(fork_ret == -1)
exit(1);
if(fork_ret == 0){
MYshell();
exit(0);
}
if(fork_ret>0){
wait(NULL);
exit(0);
}
wait(NULL);
exit(EXIT_SUCCESS);
}
void MYshell(void){
char name[BUFSIZ];
int fd,dup_ret;
sprintf(name,"%d.log",getpid());
fd = open(name, O_CREAT|O_RDWR , S_IRWXU|S_IRWXG|S_IRWXO);
if(fd<0){
printf("Error in opening or creating %s.\n",name);
exit(1);
}
dup_ret = dup2(fd,1);
if(dup_ret<0){
printf("Error in duplicating stdout descriptor.\n");
exit(1);
}
system(cmd);
close(fd);
exit(0);
}
I have started using pipe for a basic exercise. From processes A and B we get 2 random numbers. Process C does the sum of these 2 numbers and it prints it.
The problem is that I don't get the correct values from reading. The values that enter in the pipe are not the ones that exit.
What is the problem here?
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(){
int ac[2],bc[2];
int f,g;
if(pipe(ac)<0 || pipe(bc)<0){
perror("Pipe error");
exit(1);
}
f=fork();
if(f<0){
perror("Error fork1");
exit(2);
}
if(f==0){
g=fork();
if(g<0){
perror("Error fork2");
exit(3);
}
if(g==0){
printf("We are in A \n");
int variab=rand()%11+10;
printf("Here we found the number %d\n",variab);
write(ac[1],&variab,sizeof(int));
}
else{
wait(0);
printf("We are in B\n");
int variab2=rand()%6+5;
printf("Here we found the number %d\n",variab2);
write(bc[1],&variab2,sizeof(int));
}
}
else{
wait(0);
printf("We are in C\n");
int suma;
int s1=read(ac[0],&s1,sizeof(int));
printf("S1=%d\n",s1);
int s2=read(bc[0],&s2,sizeof(int));
printf("S2=%d\n",s2);
suma=s1+s2;
printf("Sum is %d\n",suma);
}
close(ac[0]);
close(ac[1]);
close(bc[0]);
close(bc[1]);
return 0;
}
The problem is with the lines
int s1=read(ac[0],&s1,sizeof(int));
...
int s2=read(bc[0],&s2,sizeof(int));
You are overwriting the value you want to get with the amount of bytes received by the read operation.
Make sure not to assign a new value to s1 and s2 by breaking up the line
int s1;
read(ac[0],&s1,sizeof(int));
(and similarly with s2) and it will work.
Question asked again and code modified...
I need to create three programs named as program0 program1 and program2 in linux.
Program0:Creates a parent with two child processes and executes program 1 and program 2 with its childs waits them to finish and close.
Program1:Takes a file name from the user and writes text to the file.It finishes writing when CTNL+D pressed and creates a pipe.After that by using cat command it writes file to stdout and uses dup() to create pipe which has file in it.
Program2:It reads filename from the pipe with the help of dup() and then executes wc command.
So far I managed to create all programs and I have no compling errors.Program 0 executes both programs.Program1 is also working and sends file to the pipe but program2 cannot read it from the pipe is prints weird symbols..
When I try to read from the pipe within the program1 it works(see the deactivated code in program1) but same code is not working if I put it inside program2.
So what how can I make program2 to read from the pipe after that I will try to execute wc command in program2 but first I should be able to see that its taking file input from the stdout so how?
I know its kinda long but please help me guys...
Program 0
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#define MAX 999
int main()
{
pid_t pid1, pid2;
pid1 = fork();
if(pid1<0)
{
fprintf(stderr,"Fork basarisiz");
exit(-1);
}
else if (pid1 ==0)/*child prosesleri*/
{
printf("program1\n");
execlp("./program1","program1",NULL);
execlp("./program2","program2",NULL);
}
else /*parent procsesleri */
{
wait(NULL);
pid2 = fork();
if(pid2<0)
{
fprintf(stderr,"Fork basarisiz");
exit(-1);
}
else if (pid2 ==0)/*child prosesleri*/
{
printf("\n");
printf("Program 2\n");
printf("\n");
execlp("./program2","program2",NULL);
//printf("\n");
}
else
{
}
/////////////////////////////////////////////////////////////////////////
wait(NULL);
printf("\n");
printf("Parent:Two child processes have successfully been created\n");
printf("Parent:Two child processes have successfully been terminated\n");
printf("Parent:This process will now terminate\n");
printf("\n");
exit(0);
}
}
Program 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#define MAX 999
int main()
{
char c[10000];
char file[10000];
int words;
printf("Child1:A text file will be created\n");
printf("Child1:Enter the name of the file\n");
scanf("%123s",file);
strcat(file,".txt");
FILE * pf;
pf = fopen(file, "w" );
if (!pf)
fprintf( stderr, "I couldn't open the file.\n" );
else
{
printf("Child1: Input a number of text lines ended, each ended by a CR (carriage return).\n");
/////////////////////////////
do
{
if (NULL != fgets(c, sizeof(c), stdin))
{
if (0 == strcmp(c, ".\n"))
{
break;
}
fprintf(pf, "%s", c);
}
else
{
if (0 != ferror(stdin))
{
fprintf(stderr, "An error occured while reading from stdin\n");
}
else
{
printf("Child1: Finish the input by CNTL^D\n");
}
break;
}
} while (1);
/////////////////////////////
}
printf("\nChild1:The file %s is succesfully created and saved in the current dictionary\n",file);
//////////////////////////////////////////////
/////////////////////////pipe///////////////
fclose(pf); // close file
char ch;
int outcount = 0;
int fd[2], nbytes;
pid_t childpid;
int i;
char f2[2];
char readbuffer[80];
pipe(fd);
if((childpid = fork()) == -1)
{
perror("fork");
exit(1);
}
if(childpid == 0)
{ printf("\nChild1:The file written to pipe with cat\n");
close(1) ;
dup(fd[1]);
close(fd[0]);
execlp("/bin/cat", "cat", file,NULL);
}
else
{
wait(NULL);
//close(0) ;
//dup(fd[0]) ;
//close(fd[1]);
//nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
//printf("%s\n",readbuffer);
}
return(0);
}
Program 2
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int fd[2],nbytes;
pid_t childpid;
char readbuffer[80];
pipe(fd);
if((childpid = fork()) == -1)
{
perror("fork");
exit(1);
}
if(childpid == 0)
{
}
else
{
close(0) ;
dup(fd[0]) ;
close(fd[1]);
nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
printf("%s\n",readbuffer);
}
return(0);
}
You may want to check the man pages for execve(2) (for starting cat) and dup2(2) (for overriding stdin and stdout as needed) for this one. execve will overwrite the currently executing program by a different one (same PID, same file descriptors), while dup2 will allow you re-define any of the standard file descriptors to point into any file descriptor you provide to it (such as any of the ends of your pipe).
I'm trying to code an exercise in C Linux where I have one semaphore with 2 spots and "n" processes entered by argument. I need that the first 2 processes use the semaphore using the 2 spots for 5 secs each and then leave the semaphore for the other remaining processes to do their stuff. The problem is that not all the other processes wait for the semaphore to be free and some of them show a semaphore error (look at the results at the bottom). I believe the problem is on the waits for the child processes, because I have a waitipid and a wait function, but I need that if there's a free spot in the semaphore, any child process running could use it. Here's the code:
//gcc SemaphoreExample.c -o s
//./s 5
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/ipc.h>
#include <sys/sem.h>
void semaphoreTask(int semid, int semnum, int semBusy)
{
struct sembuf data;
data.sem_num = semnum;
data.sem_flg = 0;
data.sem_op = semBusy;
if(semop(semid,&data,1) == -1)
{
printf("\nSemaphore Error\n");
exit(-1);
}
}
int main(int argc, char *argv[])
{
int i, fdSemaphore, quantity, fdsemctl, j;
pid_t pid[15];
system("clear");
if(argc-1 < 1)
{
printf("Some arguments are missing\n");
return EXIT_FAILURE;
}
printf("Number of arguments entered:\n\nargc: %d\n\nValues from the arguments:\n\n",argc-1);
for(i=0;i<argc;i++)
{
printf("argv[%d]: %s\n",i,argv[i]);
}
printf("\n\n");
fdSemaphore = semget(1234,1,IPC_CREAT|0777);
if(fdSemaphore == -1)
{
printf("\nError creating the Semaphore\n");
return EXIT_FAILURE;
}
fdsemctl = semctl(fdSemaphore,0,SETVAL,2);
if(fdsemctl == -1)
{
printf("\nError opening the Semaphore\n");
return EXIT_FAILURE;
}
quantity = atoi(argv[1]);
for(i=0;i<quantity;i++)
{
pid[i] = fork();
if(pid[i] == -1)
{
printf("\nError creating the Child Process\n");
return EXIT_FAILURE;
}
if(pid[i] == 0)
{
semaphoreTask(fdSemaphore,0,-1);
printf("\n[%d] I go to sleep\n",getpid());
sleep(5);
printf("\n[%d] I wake up\n",getpid());
semaphoreTask(fdSemaphore,0,1);
}
else
{
//printf("\nJust wait\n");
waitpid(pid[i],NULL,WNOHANG);
}
}
for(j=0;j<quantity;j++)
{
wait(NULL);
}
semctl(fdSemaphore,0,IPC_RMID);
return EXIT_SUCCESS;
}
This is the result I got:
Result:
Number of arguments entered:
argc: 1
Values from the arguments:
argv[0]: ./s
argv[1]: 5
[2845] I go to sleep
[2844] I go to sleep
[2845] I wake up
[2844] I wake up
Semaphore Error
[2843] I go to sleep
Semaphore Error
Semaphore Error
[2843] I wake up
Semaphore Error
Should I use wait or waitpid only?
The problem is semaphores are getting removed by forked children.
After line
semaphoreTask(fdSemaphore,0,1);
add
exit(0);
It is much simpler to implement what you want if you use sem_post and sem_wait calls. I am on a OpenBSD system and I am assuming that Linux has the same thing.