FORK ERROR in prog_limit and mem2swap:
authorMarkus Bröker <mbroeker@largo.dyndns.tv>
Thu, 16 Apr 2009 12:49:13 +0200
changeset 64 993b97c4ad2d
parent 63 5a82f89d607e
child 65 76514757b0d6
FORK ERROR in prog_limit and mem2swap: execve overrides the current PID committer: Markus Bröker <mbroeker@largo.homelinux.org>
.gitignore
Makefile
mem2swap.c
parser/c_compiler/include/prototypes.h
prog_limit.c
new file mode 100644
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,6 @@
+*~
+*.[oa]
+*.so*
+*.dll
+*.bak
+*.obj
--- a/Makefile
+++ b/Makefile
@@ -290,6 +290,11 @@
 .PHONY: clean uninstall
 
 clean:
-	rm -f *.o *~
+	find -name '*~' -exec rm -f {} \;
+	find -name '*.[oa]' -exec rm -f {} \;
 	rm -f $(TARGET)
 
+beauty:
+	find -name '*.[ch]' -exec indent {} \;
+	find -name '*.[ch]' -exec eraser {} \;
+	@make clean
--- a/mem2swap.c
+++ b/mem2swap.c
@@ -9,6 +9,7 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <errno.h>
+#include <wait.h>
 
 #define N 1000
 
@@ -29,6 +30,7 @@
 
     int size = sizeof (*p);
     int megs = DEFAULT_MEGS;
+    int pid;
 
     if (argc == 2)
         megs = atoi (argv[1]);
@@ -64,7 +66,17 @@
     }
 
     printf ("\n\n");
-    execve ("/usr/bin/free", args, NULL);
 
-    return EXIT_SUCCESS;
+    pid = fork ();
+    switch (pid) {
+    case 0:
+        execve ("/usr/bin/free", args, NULL);
+    case -1:
+        perror ("Fork Error");
+        return EXIT_FAILURE;
+    default:
+        wait (&pid);
+    }
+
+    return pid;
 }
--- a/parser/c_compiler/include/prototypes.h
+++ b/parser/c_compiler/include/prototypes.h
@@ -1,14 +1,13 @@
 #ifndef PROTOTYPES_H
 #define PROTOTYPES_H
 
-int yyparse();
-int yylex();
-void yyerror(char*);
-int yywrap();
-int count();
-int check_type();
-void comment();
+int yyparse ();
+int yylex ();
+void yyerror (char *);
+int yywrap ();
+int count ();
+int check_type ();
+void comment ();
 
 #define YYSTYPE char*
 #endif
-
--- a/prog_limit.c
+++ b/prog_limit.c
@@ -5,6 +5,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <wait.h>
 #include <unistd.h>
 
 int set_limit (int);
@@ -12,6 +13,7 @@
 int main (int argc, char **argv)
 {
     int i;
+    int pid;
 
     char **args;
 
@@ -31,11 +33,20 @@
 
     args[i - 2] = NULL;
 
-    if (set_limit (atoi (argv[1])) == 0) {
-        i = execvp (argv[2], args);
-    } else {
-        perror ("Limit Error");
+    pid = fork ();
+    switch (pid) {
+    case 0:
+        if (set_limit (atoi (argv[1])) == 0) {
+            i = execvp (argv[2], args);
+        } else {
+            perror ("Limit Error");
+            return EXIT_FAILURE;
+        }
+    case -1:
+        perror ("Fork Error");
         return EXIT_FAILURE;
+    default:
+        wait (&i);
     }
 
     if (args != NULL)