printf("ho_tari\n");
5일차 본문
2025.06.17
07.myexecl.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <wait.h>
int main(void) {
pid_t pid;
int status;
switch (pid=fork()) {
case -1:
perror("fork failed");
break;
case 0: // child process
printf("CHILD PROCESS : %d\n", getpid());
if(execl("./01.mytask", "01.mytask", "10", (char *) 0) == -1)
perror("execl");
default:
sleep(1);
system("ps -lf");
pid = wait(&status);
printf("parent : waited (%d) and completed!\n", pid);
exit(0);
}
return 0;
}

07-1.myexecl.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <wait.h>
void handler(int signo) {
pid_t exit_pid;
printf("\nhandler_pid [%d], handler_ppid [%d]\n", getpid(), getppid());
exit_pid = waitpid(-1, NULL, WNOHANG);
if(exit_pid == -1)
perror("waitpid");
else
printf("Terminated child PID = %d\n", exit_pid);
}
int main(void) {
pid_t pid;
signal(SIGCHLD, handler);
switch (pid=fork()) {
case 0: // child process
printf("CHILD PROCESS : %d\n", getpid());
char *args[] = {"01.mytask", "10", NULL};
if(execv("./01.mytask", args) == -1)
perror("execv");
exit(100);
default:
getchar();
}
return 0;
}

08.mydaemon.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <syslog.h>
#include <fcntl.h>
#include <stdlib.h>
#include <time.h>
#include <syslog.h>
int main(void) {
pid_t pid, sid;
int fd, fd0, fd1, fd2;
time_t now;
if((pid = fork()) != 0) {
exit(0); //parent exit
}
sid=setsid();
umask(0022);
chdir("/");
close(STDIN_FILENO); close(STDOUT_FILENO); close(STDERR_FILENO);
fd = open("/tmp/mydaemon.log", O_WRONLY | O_CREAT | O_TRUNC, 0666);
dup2(fd, 1); dup2(fd, 2);
while(1){
time(&now);
fprintf(stdout, "Mydaemon alive at %s", ctime(&now));
fflush(stdout); /* flush the stream */
sleep(5);
}
return 0;
}


09.mydaemon.service
[Unit]
Description=Mydaemon testing via by systemd
[Service]
ExecStart=/usr/local/bin/09.mydaemon-systemd
Restart=on-failure
Type=forking
PIDFile=/run/mydaemon.pid
[Install]
WantedBy=multi-user.target
09.mydaemon-systemd.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <syslog.h>
#include <fcntl.h>
#include <stdlib.h>
#include <time.h>
#include <signal.h>
void sigHandler(int sig);
int fd;
const char pidfile[] = "/run/mydaemon.pid";
const char daemonlog[]="/tmp/mydaemon.log";
int main(int argc, char * argv[]) {
pid_t pid, sid;
time_t now;
struct sigaction action;
if((pid = fork()) != 0) {
exit(0); //parent exit and child continues
}
if((pid = fork()) !=0 ) { //double forking
exit(0);
}
sid=setsid();
if ((fd = open(pidfile, O_RDWR | O_CREAT)) == -1){
perror("Can't open file for writing");
return 1;
}
dprintf(fd, "%d\n", getpid());
umask(0);
if(chdir("/") !=0 ) {
perror("chdir");
exit(1);
}
close(STDIN_FILENO); close(STDOUT_FILENO); close(STDERR_FILENO);
/* prepare for sigaction */
action.sa_handler = sigHandler;
sigfillset(&action.sa_mask);
action.sa_flags = SA_RESTART;
/* register the signals we want to handle */
sigaction(SIGTERM, &action, NULL);
sigaction(SIGINT, &action, NULL);
sigaction(SIGQUIT, &action, NULL);
sigaction(SIGABRT, &action, NULL);
if((access(daemonlog, F_OK)) == 0){
unlink(daemonlog);
}
if ((fd = open(daemonlog, O_CREAT | O_RDWR | O_TRUNC, 0644)) == -1){
perror("Can't open daemonlog");
return 1;
}
while(1){
time(&now);
dprintf(fd, "Mydaemon alive at %s", ctime(&now));
sleep(5);
}
return 0;
}
void sigHandler(int sig){
int status = 0;
if ( sig == SIGTERM || sig == SIGINT || sig == SIGQUIT || sig == SIGABRT ){
if ((unlink(pidfile)) == -1) /* remove the pid-file */
status = 1;
if ((close(fd)) == -1)
status = 1;
if ((unlink(daemonlog)) == -1) /* remove the daemonlog */
status = 1;
exit(status); /* exit with the status set*/
}else{ /* some other signal */
exit(1);
}
}


01.mypipe1.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <time.h>
int main(void) {
int pd[2], read_fd, write_fd;
pid_t pid;
time_t timer1, timer2;
char tx_buf[100], rx_buf[100];
if ( pipe(pd) == -1 ) {
perror("pipe");
exit(1);
}
read_fd = pd[0];
write_fd = pd[1];
switch(pid=fork()) {
case 0:
close(read_fd);
for(int i=0; i<11; i++){
// for(timer1=time(NULL); time(NULL)<timer1 + 1;)
// continue;
// strcpy(tx_buf, "\e[31mHello Parent. I am child.");
sprintf(tx_buf, "\e[31mHello Parent. I am child ---%d\n", i);
write(write_fd, tx_buf, strlen(tx_buf)+1);
for(timer1=time(NULL); time(NULL)<timer1 + 1;)
continue;
// read(read_fd, rx_buf, sizeof(rx_buf));
// printf("\e[00m--------> CHILD: %s\n", rx_buf);
}
exit(0);
default:
#if 1
close(write_fd);
for(int i=0; i<10; i++){
// for(timer2=time(NULL); time(NULL)<timer2 + 2;)
// continue;
// memset(&rx_buf[0], 0, sizeof(rx_buf));
read(read_fd, rx_buf, sizeof(rx_buf));
printf("\e[00mPARENT: %s\n", rx_buf);
// for(timer2=time(NULL); time(NULL)<timer2 + 2;)
// continue;
// strcpy(tx_buf, "\e[00mHello Child. I am Parent");
// sprintf(tx_buf, "\e[00mWeleome Child. I am Parent--%d\n", i);
// write(write_fd, tx_buf, strlen(tx_buf)+1);
}
#else
for(int i=0; i<10; i++){
for(timer2=time(NULL); time(NULL)<timer2 + 2;)
continue;
strcpy(tx_buf, "\e[00mHello Child. I am Parent");
write(write_fd, tx_buf, strlen(tx_buf)+1);
read(read_fd, rx_buf, sizeof(rx_buf));
printf("\e[00mPARENT: %s\n", rx_buf);
}
#endif
exit(0);
}
}

02.mypipe2.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#define MSGSIZE 64
int main(void){
int pd1[2], pd2[3];
pid_t ret;
time_t timer1;
char sbuf[MSGSIZE];
char rbuf[MSGSIZE];
int i, len;
if(pipe(pd1) == -1){
perror("pipe1");
exit(1);
}
if(pipe(pd2) == -1){
perror("pipe2");
exit(1);
}
switch(fork()){
case 0: //child
close(pd1[0]);
close(pd2[1]);
for(i=0; i<5; i++) {
sprintf(&sbuf[0], "Hello, Parent #%d", i+1);
write(pd1[1], sbuf, strlen(sbuf));
memset(&rbuf[0], 0, MSGSIZE);
len = read(pd2[0], rbuf, MSGSIZE);
if(len == 0){
break;
}
printf("CHILD: %s\n", rbuf);
for(timer1=time(NULL); time(NULL)<timer1 + 1;)
continue;
}
exit(0);
default: //parent
close(pd1[1]);
close(pd2[0]);
for(i=0; ; i++) {
memset(&rbuf[0], 0, MSGSIZE);
len = read(pd1[0], rbuf, MSGSIZE);
if(len == 0){
break;
}
printf("PARENT: %s\n", rbuf);
for(timer1=time(NULL); time(NULL)<timer1 + 1;)
continue;
sprintf(&sbuf[0], "Welcome, Child #%d", i+1);
write(pd2[1], sbuf, strlen(sbuf));
}
}
return 0;
}

03.myfifo_recv.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
int fd;
char buf[128];
int count = 0;
if((access ("/tmp/myfifo", F_OK)) != 0){
if(mkfifo("/tmp/myfifo", S_IRUSR | S_IWUSR) == -1){
perror("mkfifo");
exit(1);
}
}
if((fd = open("/tmp/myfifo", O_RDONLY)) == -1){
perror("open");
exit(1);
}
while(1){
memset(buf, 0, sizeof(buf));
read(fd, buf, sizeof(buf));
printf("Rx - %s\n", buf);
if(strstr(buf, "end")){
break;
}
}
close(fd);
unlink("/tmp/myfifo");
return 0;
}
04.myfifo_send.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int main(){
int fd, i;
char buf[128];
time_t timer1;
if((fd = open("/tmp/myfifo", O_WRONLY)) == -1){
perror("open");
exit(2);
}
for(i=0; i<5; i++){
memset(buf, 0, sizeof(buf));
sprintf(&buf[0], "Hello(%d)", i);
write(fd, &buf[0], strlen(buf)+1);
printf("Tx: %s\n", buf);
for(timer1=time(NULL); time(NULL)<timer1 + 2;)
continue;
}
memset(buf, 0, sizeof(buf));
sprintf(buf, "end");
write(fd, buf, strlen(buf)+1);
close(fd);
/* unlink("/tmp/mkfifo"); */
return 0;
}


03.posix_shm_write.c
#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#define MEM_SIZE 128
int main() {
int fd;
void *shm_addr;
const char *message0= "Welcome to ";
const char *message1= "Linux Systems ";
const char *message2= "Programming!";
fd = shm_open("/mydata", O_RDWR | O_CREAT, 0666);
ftruncate(fd, MEM_SIZE);
shm_addr = mmap(0, MEM_SIZE, PROT_WRITE, MAP_SHARED, fd, 0);
printf( "Map addr is %p\n", shm_addr );
#if 1
write(fd, message0, strlen(message0));
write(fd, message1, strlen(message1));
write(fd, message2, strlen(message2));
#else
memcpy(shm_addr, message0, strlen(message0));
shm_addr += strlen(message0);
sprintf(shm_addr, message1, strlen(message1));
shm_addr += strlen(message1);
memcpy(shm_addr, message2, strlen(message2));
shm_addr += strlen(message2);
#endif
printf("Press enter to munmap.... ");
getchar();
munmap(shm_addr, MEM_SIZE);
return 0;
}
04.posix_shm_read.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#define MEM_SIZE 128
int main() {
int fd;
void *shm_addr;
char buf[MEM_SIZE];
fd = shm_open("/mydata", O_RDONLY, 0666);
if(fd == -1){
perror("shm_open");
exit(1);
}
shm_addr = mmap(0, MEM_SIZE, PROT_READ, MAP_SHARED, fd, 0);
if(shm_addr == (void *)-1){
perror("mmap error");
return EXIT_FAILURE;
}
#if 1
memset(buf, 0, MEM_SIZE);
read(fd, buf, MEM_SIZE);
printf("%s\n", buf);
#else
memcpy(buf, shm_addr, sizeof(buf));
printf("Map addr is %p\n", shm_addr);
printf("Read message: %s\n", buf);
#endif
printf("Press enter to munmap .....");
getchar();
munmap(shm_addr, MEM_SIZE);
printf("Press enter to remove shared memory file");
getchar();
shm_unlink("/mydata");
// close(fd);
return 0;
}
03.posix-mqsend.c
#include <stdio.h>
#include <mqueue.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#define MAX_MSG_SIZE 2048
int main(int argc, char *argv[]){
int mqd; /* msg queue descriptor */
/* attributes for the message queue */
struct mq_attr msgattr;
msgattr.mq_maxmsg = 10;
msgattr.mq_msgsize = MAX_MSG_SIZE;
char msg[128];
mqd = mq_open("/myqueue", O_CREAT|O_RDWR, 0644, &msgattr);
if(mqd == -1){
perror("Creating message queue");
return 1;
}
for(int i=0; i<10; i++){
sprintf(&msg[0], "Hello, message queue #%d", i+1);
if((mq_send(mqd, msg, strlen(msg), 1)) == -1){
perror("Message queue send");
return 1;
}
}
printf("Press Enter to remove message queue....");
getchar();
mq_close(mqd);
mq_unlink("/myqueue");
return 0;
}
03.posix-mqrecv.c
#include <stdio.h>
#include <mqueue.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
int main(void){
int mqd; /* msg queue descriptor */
char *buffer;
struct mq_attr msgattr;
mqd = mq_open("/myqueue", O_RDONLY);
if(mqd == -1){
perror("Open message queue");
return 1;
}
if((mq_getattr(mqd, &msgattr)) == -1){
perror("Get message attribute");
return 1;
}
buffer = calloc(msgattr.mq_msgsize, sizeof(char));
if(buffer == NULL){
fprintf(stderr, "Couldn't allocate memory");
return 1;
}
printf("%ld messages in queue\n", msgattr.mq_curmsgs);
for(int i = 0; i<msgattr.mq_curmsgs; i++){
if((mq_receive(mqd, buffer, msgattr.mq_msgsize, NULL)) == -1){
perror("Message receive");
return 1;
}
printf("%s\n", buffer);
}
memset(buffer, '\0', msgattr.mq_msgsize);
free(buffer);
mq_close(mqd);
//mq_unlink("/myqueue");
return 0;
}
03.shm_posix_named_sem1.c
#include <semaphore.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/shm.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <stdio.h>
#define KEY_NUM 0x2222
#define MEM_SIZE 1024
int main(int argc, char **argv) {
sem_t *mysem;
int shm_id;
void *shm_addr;
printf("Started --------------------\n");
if((shm_id = shmget((key_t)KEY_NUM,MEM_SIZE,IPC_CREAT|0666)) == -1) {
perror("shmget");
exit(1);
}
if((shm_addr = shmat(shm_id, NULL, 0)) == (void *)-1) {
perror("shmat");
exit(1);
}
printf("Named semaphore ---------------\n");
if((mysem = sem_open("/mysemaphore", O_CREAT, 0777, 1)) == NULL) {
perror("Sem Open Error");
return 1;
}
for(int i=0; i<500; i++){
#ifdef SEM
sem_wait(mysem);
#endif
sprintf((char *)shm_addr, "%d", getpid());
for(int j=0; j<5000000; j++){}
if(getpid() == atoi(shm_addr))
putchar('0');
else
putchar('X');
fflush(stdout);
#ifdef SEM
sem_post(mysem);
#endif
}
if(shmdt(shm_addr) !=0){
perror("shmdt");
exit(2);
}
sem_close(mysem);
sem_unlink("/mysemaphore");
if(shmctl(shm_id, IPC_RMID, NULL) == -1){
perror("shmctl");
exit(2);
}
}
// sem.mysem file for mysem semaphore is located in /dev/shm
// To compile the program, -lrt & -lpthread is required

04.shm_posix_named_sem2.c
#include <semaphore.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/shm.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <stdio.h>
#define KEY_NUM 0x2222
#define MEM_SIZE 1024
int main(int argc, char **argv){
sem_t *mysem;
int shm_id;
void *shm_addr;
printf("Started --------------------\n");
if((shm_id = shmget((key_t)KEY_NUM,MEM_SIZE,IPC_CREAT|0666)) == -1) {
perror("shmget");
exit(1);
}
if((shm_addr = shmat(shm_id, NULL, 0)) == (void *)-1) {
perror("shmat");
exit(1);
}
printf("Named semaphore ---------------\n");
if((mysem = sem_open("/mysemaphore", O_CREAT, 0777, 1)) == SEM_FAILED) {
perror("Sem Open Error");
return 1;
}
for(int i=0; i<500; i++){
#ifdef SEM
sem_wait(mysem);
#endif
sprintf((char *)shm_addr, "%d", getpid());
for(int j=0; j<3000000; j++){}
if(getpid() == atoi(shm_addr))
putchar('0');
else
putchar('X');
fflush(stdout);
#ifdef SEM
sem_post(mysem);
#endif
}
if(shmdt(shm_addr) !=0){
perror("shmdt");
exit(2);
}
sem_close(mysem);
sem_unlink("/mysemaphore");
}

'(Telechips) AI 시스템 반도체 SW 개발자 교육 > SoC 시스템 반도체를 위한 임베디드 리눅스' 카테고리의 다른 글
| 7일차 (0) | 2025.06.20 |
|---|---|
| 6일차 (0) | 2025.06.18 |
| 4일차 (0) | 2025.06.16 |
| 3일차 (0) | 2025.06.13 |
| 1일차 (0) | 2025.06.10 |