Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <unistd.h>
- #include <syslog.h>
- #include <string.h>
- int main() {
- pid_t pid, sid; // Variabel untuk menyimpan PID
- pid = fork(); // Menyimpan PID dari Child Process
- /* Keluar saat fork gagal
- * (nilai variabel pid < 0) */
- if (pid < 0) {
- exit(EXIT_FAILURE);
- }
- /* Keluar saat fork berhasil
- * (nilai variabel pid adalah PID dari child process) */
- if (pid > 0) {
- exit(EXIT_SUCCESS);
- }
- umask(0);
- sid = setsid();
- if (sid < 0) {
- exit(EXIT_FAILURE);
- }
- if ((chdir("/")) < 0) {
- exit(EXIT_FAILURE);
- }
- close(STDIN_FILENO);
- close(STDOUT_FILENO);
- close(STDERR_FILENO);
- while (1) {
- // Tulis program kalian di sini
- pid_t child_id;
- int status;
- if (child_id == 0) {
- // this is child
- char *argv[] = {"mkdir", "-p", "folderku", NULL};
- execv("/bin/mkdir", argv);
- } else {
- // this is parent
- while ((wait(&status)) > 0);
- char *argv[] = {"touch", "folderku/fileku.txt", NULL};
- execv("/usr/bin/touch", argv);
- }
- sleep(30);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment