Ich versuche, drei execv ("./Test", execv_str) parallel zu laufen. Und ich muss Erfolgsmeldung ausdrucken, wenn jede von execv() erfolgreich abgeschlossen wird.fork() und exec() laufen parallel in C
Aber jetzt bekomme ich Ergebnis wie folgt:
[email protected]:~/Desktop/$./test -p
SUCCESS
SUCCESS
SUCCESS
[email protected]:~/Desktop/$ TESTING
TESTING
TESTING
Das erwartete Ergebnis wird sein:
[email protected]:~/Desktop/$./test -p
TESTING
SUCCESS
TESTING
SUCCESS
TESTING
SUCCESS
[email protected]:~/Desktop/$
Hier ist der Code ist.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int fork_execv()
{
int status;
pid_t pid;
pid = fork();
/* Handling Child Process */
if(pid == 0){
char* execv_str[] = {"./test", NULL};
if (execv("./test",execv_str) < 0){
status = -1;
perror("ERROR\n");
}
}
/* Handling Child Process Failure */
else if(pid < 0){
status = -1;
perror("ERROR\n");
}
return status;
}
int main(int argc, char *argv[]){
if (argc == 1){
sleep(5);
printf("TESTING\n");
}
else{
int i;
for(i = 0; i < 3; ++i){
if (fork_execv() != -1){
printf("SUCCESS\n");
}
}
}
}
Wie kann ich meinen Code ändern, damit er funktioniert?
C unterstützt Multithreading nicht –
@DepeshChoudhary - Diese Frage beinhaltet keine Threads. (Und beachten Sie, dass C in der Tat Threads unterstützt.) –
@ Oliver Charlesworth Wirklich? Bitte sag mir wie (oder teile einen Link). Das wollte ich schon lange in c verwenden. –