Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // -------------------- System Call ------------
- //mysys.c
- #include <stdio.h>
- int main(int argc, char *argv[]) {
- int status;
- if (argc < 2) {
- printf("\nCommand-line argument required!\n");
- exit(0);
- }
- if ((status = system(argv[1])) < 0)
- perror("system() error");
- return 0;
- }
- //showpid.c
- #include <stdio.h>
- int main(void) {
- printf("Real UID = %d,
- Eff UID = %d\n",
- getuid(), geteuid());
- return 0;
- }
- //-------------------- Signal --------------
- //signal1.c
- #include <signal.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- void my_alarm(int sig) {
- for (int i = 0; i < 5; i++)
- printf("Alarm!!! \n");
- }
- int main(void) {
- int pid;
- printf("Alarm clock is starting...\n");
- if ((pid = fork()) == 0) {
- sleep(3);
- kill(getppid(), SIGALRM);
- exit(0);
- }
- printf("Waiting for alarm...\n");
- (void) signal(SIGALRM, my_alarm);
- pause();
- printf("Done!\n");
- exit(0);
- }
- //signal2.c
- #include <sys/types.h>
- #include <signal.h>
- #include <unistd.h>
- #include <stdio.h>
- /* one handler for both signals */
- static void sig_usr(int);
- int main(void) {
- if (signal(SIGUSR1, sig_usr) == SIG_ERR)
- perror("can't catch SIGUSR1");
- if (signal(SIGUSR2, sig_usr) == SIG_ERR)
- perror("can't catch SIGUSR2");
- for ( ; ; )
- pause();
- }
- /* argument is signal number */
- static void sig_usr(int signo) {
- if (signo == SIGUSR1)
- printf("received SIGUSR1\n");
- else if (signo == SIGUSR2)
- printf("received SIGUSR2\n");
- else
- printf("received signal %d\n", signo);
- }
- //signal3.c
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <signal.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- static void sig_cld(int);
- int main()
- {
- pid_t pid;
- if (signal(SIGCLD, sig_cld) == SIG_ERR)
- perror("signal error");
- if ((pid = fork()) < 0)
- perror("fork error");
- else if (pid == 0)
- {
- /* child */ sleep(2);
- _exit(0);
- }
- pause(); /* parent */
- exit(0);
- }
- static void sig_cld(int signo)
- /* interrupts pause() */
- {
- pid_t pid;
- int status;
- printf("SIGCLD received\n");
- /* reestablish handler */
- if (signal(SIGCLD, sig_cld) == SIG_ERR)
- perror("signal error");
- /* fetch child status */
- if ((pid = wait(&status)) < 0)
- perror("wait error");
- printf("pid = %d\n", pid);
- return;
- }
- //sigaction.c
- #include <signal.h>
- #include <stdio.h>
- #include <unistd.h>
- void rx_int(int sig)
- {
- printf("SIGINT - %d Received!!!\n", sig);
- }
- int main(void)
- {
- struct sigaction act;
- act.sa_handler = rx_int;
- sigemptyset(&act.sa_mask);
- act.sa_flags = 0;
- sigaction(SIGINT, &act, 0);
- while(1)
- {
- printf("Running…\n");
- sleep(1);
- }
- }
- //killthatshit.c
- #include <signal.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <stdlib.h>
- #include <string.h>
- int main(void)
- {
- int total_length = 20;
- char line[total_length];
- FILE * command = popen("pidof sigaction","r");
- fgets(line,total_length,command);
- pid_t pid = strtoul(line,NULL,10);
- pclose(command);
- printf("PID IS : %d\n", pid);
- kill(pid, SIGTERM);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement