int sched_setscheduler(pid_t pid, int policy, const struct sched_param *param); int sched_getscheduler(pid_t pid);
int sched_get_priority_max(int policy); int sched_get_priority_min(int policy);
int sched_setparam(pid_t pid, const struct sched_param *param); int sched_getparam(pid_t pid, struct sched_param *param);
int sched_yield(void);
int sched_setaffinity(pid_t pid, size_t cpusetsize, cpu_set_t *mask); int sched_getaffinity(pid_t pid, size_t cpusetsize, cpu_set_t *mask);
int nice(int inc);
/* Copyright (C) HWPORT.COM All rights reserved. Author: JAEHYUK CHO <mailto:minzkn@minzkn.com> */ #if !defined(_ISOC99_SOURCE) # define _ISOC99_SOURCE (1L) #endif #if !defined(_GNU_SOURCE) # define _GNU_SOURCE (1L) #endif #include <sys/types.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <sched.h> int main(int s_argc, char **s_argv); int main(int s_argc, char **s_argv) { pid_t s_pid; cpu_set_t s_cpu_set; int s_check; int s_cpu_count; int s_current_cpu_running_index; int s_cpu_index; s_pid = getpid(); if(s_argc >= 2) { s_check = atoi(s_argv[1]); if(s_check > 0) { s_pid = (pid_t)s_check; } } else { (void)fprintf( stdout, "usage: %s <pid> [<cpuid> [...]]\n\n", basename(s_argv[0]) ); } if(s_argc >= 3) { /* 특정 CPU만 사용하도록 하는 경우 */ int s_arg_index; CPU_ZERO(&s_cpu_set); for(s_arg_index = 3;s_arg_index < s_argc;s_arg_index++) { s_cpu_index = atoi(s_argv[s_arg_index]); if(__builtin_expect(s_cpu_index <= 0, 0)) { (void)fprintf( stderr, "invalid argument ! (argv[%d]=\"%s\")\n", s_arg_index, s_argv[s_arg_index] ); } else { CPU_SET((size_t)s_cpu_index, &s_cpu_set); } } s_cpu_count = (int)CPU_COUNT(&s_cpu_set); if(__builtin_expect(s_cpu_count <= 0, 0)) { (void)fprintf(stderr, "not assigned cpu ! (invalid argument ?)\n"); } else { s_check = sched_setaffinity(s_pid, sizeof(s_cpu_set), (cpu_set_t *)(&s_cpu_set)); if(__builtin_expect(s_check == (-1), 0)) { perror("sched_setaffinity"); return(EXIT_FAILURE); } } } s_check = sched_getaffinity(s_pid, sizeof(s_cpu_set), (cpu_set_t *)(&s_cpu_set)); if(__builtin_expect(s_check == (-1), 0)) { perror("sched_getaffinity"); return(EXIT_FAILURE); } /* 주어진 pid에 해당하는 프로그램이 사용할 수 있는 사용가능한 CPU 개수 */ s_cpu_count = (int)CPU_COUNT(&s_cpu_set); /* 현재 sched_getcpu 호출시점에서 사용중인 CPU 번호 */ s_current_cpu_running_index = (int)sched_getcpu(); (void)fprintf(stdout, "CPU_USING_COUNT=%d (pid=%ld)\n", s_cpu_count, (long)s_pid); for(s_cpu_index = 0;(s_cpu_index < s_cpu_count) && (s_cpu_index < ((int)CPU_SETSIZE));s_cpu_index++) { (void)fprintf( stdout, "\tCPU%d : %s%s\n", s_cpu_index + 1, (CPU_ISSET((size_t)s_cpu_index, &s_cpu_set) == 0) ? "not used" : "used", (s_cpu_index == s_current_cpu_running_index) ? " (current running)" : "" ); } return(EXIT_SUCCESS); } /* vim: set expandtab: */ /* End of source */
uid_t getuid(void); uid_t geteuid(void); int setuid(uid_t uid); int seteuid(uid_t euid); gid_t getgid(void); gid_t getegid(void); int setgid(gid_t gid); int setegid(gid_t egid); pid_t fork(void); pid_t vfork(void); int execl(const char *path, const char *arg, ...); int execlp(const char *file, const char *arg, ...); int execle(const char *path, const char *arg, ..., char * const envp[]); int execv(const char *path, char *const argv[]); int execvp(const char *file, char *const argv[]); void exit(int status); pid_t wait(int *status); pid_t waitpid(pid_t pid, int *status, int options); int system(const char *command); int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); int pthread_join(pthread_t thread, void **retval); int pthread_detach(pthread_t thread);
pid_t pid = vfork(); if(pid == ((pid_t)(-1))) perror("fork"); else if(pid == ((pid_t)0)) { /* 자식 process */ char * const arg[] = { "/bin/ls", "-al", (char *)0 }; (void)execvp(arg[0], arg); exit(0); } else (void)waitpid(pid, (int *)0, 0); /* 부모 process */
SIGHUP SIGINT SIGQUIT SIGILL SIGTRAP SIGABRT SIGIOT SIGBUS SIGFPE SIGKILL SIGUSR1 SIGSEGV SIGUSR2 SIGPIPE SIGALRM SIGTERM SIGSTKFLT SIGCHLD SIGCONT SIGSTOP SIGTSTP SIGTTIN SIGTTOU SIGURG SIGXCPU SIGXFSZ SIGVTALRM SIGPROF SIGWINCH SIGIO SIGPOLL SIGPWR SIGSYS
typedef void (*sighandler_t)(int); sighandler_t signal(int signum, sighandler_t handler); int kill(pid_t pid, int sig); int raise(int sig); int pause(void); unsigned int alarm(unsigned int seconds); /* SIGALRM */
int open(const char *pathname, int flags); int open(const char *pathname, int flags, mode_t mode); int creat(const char *pathname, mode_t mode); int close(int fd); ssize_t read(int fd, void *buf, size_t count); ssize_t write(int fd, const void *buf, size_t count); off_t lseek(int fd, off_t offset, int whence); int fcntl(int fd, int cmd, ... /* arg */ ); int ioctl(int d, int request, ...); int remove(const char *pathname); DIR *opendir(const char *name); int readdir(unsigned int fd, struct old_linux_dirent *dirp, unsigned int count); int closedir(DIR *dirp); int dup(int oldfd); int dup2(int oldfd, int newfd); int stat(const char *path, struct stat *buf); int fstat(int fd, struct stat *buf); int lstat(const char *path, struct stat *buf); int link(const char *oldpath, const char *newpath); int unlink(const char *pathname); int symlink(const char *oldpath, const char *newpath); ssize_t readlink(const char *path, char *buf, size_t bufsiz); int utime(const char *filename, const struct utimbuf *times); int mkdir(const char *pathname, mode_t mode); int rmdir(const char *pathname);
/* pipe */ int pipe(int pipefd[2]); /* stream pipe */ FILE *popen(const char *command, const char *type); int pclose(FILE *stream); /* fifo */ int mkfifo(const char *pathname, mode_t mode); int mknod(const char *pathname, mode_t mode, dev_t dev); /* 세마포어 */ int semget(key_t key, int nsems, int semflg); int semctl(int semid, int semnum, int cmd, ...); int semop(int semid, struct sembuf *sops, unsigned nsops); /* 메시지큐 */ int msgget(key_t key, int msgflg); int msgctl(int msqid, int cmd, struct msqid_ds *buf); int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg); ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg); /* 공유메모리 */ int shmget(key_t key, size_t size, int shmflg); void *shmat(int shmid, const void *shmaddr, int shmflg); int shmdt(const void *shmaddr); int shmctl(int shmid, int cmd, struct shmid_ds *buf);
#include <sys/sysinfo.h> #include <stdio.h> #include <stdlib.h> int main(int s_argc, char **s_argv) { struct sysinfo s_sysinfo_local; (void)s_argc; (void)s_argv; if(sysinfo((struct sysinfo *)(&s_sysinfo_local)) == 0) { (void)fprintf(stdout, "uptime is %ld sec\n", (long)s_sysinfo_local.uptime); } return(EXIT_SUCCESS); }
#include <stdio.h> #include <stdlib.h> #include <time.h> int main(int s_argc, char **s_argv) { time_t s_time_local; if(time((time_t *)(&s_time_local)) != ((time_t)(-1))) { struct tm s_tm_local; (void)memcpy((void *)(&s_tm_local), localtime((time_t *)(&s_time_local)), sizeof(struct tm)); (void)fprintf(stdout, "지역시간: %d/%d/%d %02d:%02d:%02d\n", s_tm_local.tm_year + 19000, s_tm_local.tm_mon + 1, s_tm_local.tm_mday, s_tm_local.tm_hour, s_tm_local.tm_min, s_tm_local.tm_sec); (void)memcpy((void *)(&s_tm_local), gmtime((time_t *)(&s_time_local)), sizeof(struct tm)); (void)fprintf(stdout, "그리니치 표준시: %d/%d/%d %02d:%02d:% 02d\n", s_tm_local.tm_year + 19000, s_tm_local.tm_mon + 1, s_tm_local.tm_mday, s_tm_local.tm_hour, s_tm_local.tm_min, s_tm_local.tm_sec); } return(EXIT_SUCCESS); }
#include <time.h> ... struct tm s_set_tm_local; time_t s_set_time_local; (void)memset((void *)(&s_set_tm_local), 0, sizeof(s_set_tm_local)); s_set_tm_local.tm_year = <년도> - 1900; s_set_tm_local.tm_mon = <월> - 1; s_set_tm_local.tm_mday = <일>; s_set_tm_local.tm_hour = <시>; s_set_tm_local.tm_min = <분>; s_set_tm_local.tm_sec = <초>; s_set_tm_local.tm_isdst = <일광절약시간 적용유무>; #if defined(_BSD_SOURCE) s_set_tm_local.tm_gmtoff = <UTC로부터 지역시간대의 시간>; /* -timezone */ s_set_tm_local.tm_zone = <지역시간대 이름>; /* tzname[ s_set_tm_local.tm_isdst ] */ #endif s_set_time_local = mktime((struct tm *)(&s_set_tm_local)); /* struct tm구조체형식을 time_t 형식으로 변환해주며 tm구조체를 일부 보정하는 기능이 포함되어 있습니다. */ if(stime((time_t *)(&s_set_time_local)) == 0) { /* 설정완료 */ } ...
int s_tz_index; struct tm s_tm_local; time_t s_time[2]; /* [0]=standard time, [1]=daylight time */ long s_delta_zone; long s_timezone; long s_altzone; #if defined(_WIN32) /* windows의 경우 */ char s_tzname_local[2][ TZNAME_MAX ]; size_t s_tzname_size; #endif const char *s_tzname[2]; #if defined(_WIN32) /* windows의 경우 */ _tzset(); #else tzset(); #endif for(s_tz_index = 0;s_tz_index < 2;s_tz_index++) { #if defined(_WIN32) /* windows의 경우 */ (void)_get_tzname( (size_t *)(&s_tzname_size), (char *)memeset((void *)(&s_tzname_local[s_tz_index]), 0, sizeof(s_tzname_local[s_tz_index])), sizeof(s_tzname_local[s_tz_index]), s_tz_index ); s_tzname[s_tz_index] = (const char *)(&s_tzname_local[s_tz_index]); #else s_tzname[s_tz_index] = tzname[s_tz_index]; #endif (void)memset((void *)(&s_tm_local), 0, sizeof(s_tm_local)); s_tm_local.tm_year = 2012 - 1900; s_tm_local.tm_mon = 8 - 1; s_tm_local.tm_mday = 4; s_tm_local.tm_hour = 1; s_tm_local.tm_min = 30; s_tm_local.tm_sec = 0; s_tm_local.tm_isdst = s_tz_index; s_time[s_tz_index] = mktime((struct tm *)(&s_tm_local)); } /* 일광절약시간에 의한 시간차 (일광절약시간이 사용된다면 통상적으로 3600초가 나옵니다.) */ s_delta_zone = (long)(((long)s_time[0]) - ((long)s_time[1])); /* 지역시간대로부터 UTC와의 시간차 (대한민국의 경우 -32400 초 [ -9시간 ]) */ #if defined(_WIN32) /* windows의 경우 */ _get_timezone(&s_timezone); #else s_timezone = timemzone; #endif /* 일광절약시간을 함께 포함한 지역시간대로부터 UTC와의 시간차 */ s_altzone = s_timezone - s_delta_zone; /* 지역시간대 및 일광절약 표기 */ (void)printf("%s%ld%s%ld\n", s_tzname[0], s_timezone, s_tzname[1], s_altzone); /* UTC로부터의 시간차 표기 */ (void)printf("PST%ldPDT%ld (UTC%ld)\n", -s_timezone, -s_altzone, -s_timezone);
#include <sys/time.h> #include <time.h> #include <unistd.h> typedef unsigned long long __mz_time_stamp_t; #define mz_time_stamp_t __mz_time_stamp_t typedef struct mz_timer_ts { /* need shadow */ long clock_tick; clock_t prev_clock; mz_time_stamp_t time_stamp; }__mz_timer_t; #define mz_timer_t __mz_timer_t static mz_time_stamp_t __mz_timer_time_stamp(mz_timer_t *s_timer) { clock_t s_clock; #if defined(__linux__) /* get linux kernel's jiffes (tick counter) */ s_clock = times((struct tms *)0); #else do { struct tms s_tms; s_clock = times((struct tms *)(&s_tms)); }while(0); #endif if(s_clock == ((clock_t)(-1))) { /* overflow clock timing */ return(s_timer->time_stamp); } if(s_timer->clock_tick <= 0l) { /* get ticks per second */ s_timer->clock_tick = sysconf(_SC_CLK_TCK); if(s_timer->clock_tick <= 0l) { /* invalid clock tick */ return(s_timer->time_stamp); } s_timer->prev_clock = s_clock; } /* update time stamp (clock to upscale) */ s_timer->time_stamp += (((mz_time_stamp_t)(s_clock - s_timer->prev_clock)) * ((mz_time_stamp_t)1000)) / ((mz_time_stamp_t)s_timer->clock_tick); s_timer->prev_clock = s_clock; return(s_timer->time_stamp); } static mz_time_stamp_t mz_get_time_stamp_msec(mz_timer_t *s_timer) { statuc mz_timer_t g_global_timer_local = { 0l, (clock_t)0, (mz_time_stamp_t)1000u }; return(__mz_timer_time_stamp((mz_timer_t *)(&g_global_timer_local)); }
unsigned long long int mz_get_cpu_clock(void) { unsigned long long int s_qword; __asm__ volatile( "rdtsc\n\t" : "=A"(s_qword) ); return(s_qword); }
for(;;);
#define HZ 100 for(;;)usleep((HZ/1000) * 1000); /* 이 경우 HZ상수에 따른 최적의 sleep을 주는것이지만 그냥 usleep(10000) 으로 해도 무관합니다. */
/* Copyright (C) JAEHYUK CHO All rights reserved. Code by JaeHyuk Cho <mailto:minzkn@minzkn.com> */ #include <sys/types.h> #include <sys/time.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <signal.h> #include <time.h> #include <unistd.h> #include <errno.h> #include <pthread.h> #define def_mztimer_realtime_signal SIGRTMIN static long g_mztimer_interval_usec; static void mztimer_handler(int s_signal, siginfo_t *s_siginfo, void *s_user_context) { static unsigned int s_tick = 0; timer_t *s_timer_id_ptr; int s_overrun_count; (void)s_signal; (void)s_siginfo; (void)s_user_context; s_timer_id_ptr = (timer_t *)s_siginfo->si_value.sival_ptr; s_overrun_count = timer_getoverrun(*s_timer_id_ptr); if(s_overrun_count == (-1)) { s_overrun_count = 0; } (void)fprintf(stdout, "tick=%u, overrun_count=%d", s_tick, s_overrun_count); #if 1L /* DEBUG: compare with system time */ do { static unsigned long long s_start_time_usec = 0ull; unsigned long long s_current_time_usec; struct timeval s_timeval; unsigned long long s_duration_usec, s_diff_usec; s_duration_usec = s_tick * g_mztimer_interval_usec; (void)gettimeofday(&s_timeval, NULL); s_current_time_usec = (s_timeval.tv_sec * 1000000) + s_timeval.tv_usec; if(s_start_time_usec == 0ull) { s_start_time_usec = s_current_time_usec; } s_current_time_usec -= s_start_time_usec; if(s_duration_usec > s_current_time_usec) { s_diff_usec = s_duration_usec - s_current_time_usec; } else { s_diff_usec = s_current_time_usec - s_duration_usec; } fprintf(stdout, ", real=%llu.%03llu, diff=%llu.%llu", s_current_time_usec / 1000ull, s_current_time_usec % 1000ull, s_diff_usec / 1000ull, s_diff_usec % 1000ull); }while(0); #endif (void)fprintf(stdout, "\n"); s_tick += 1 + s_overrun_count; } static void *mztimer_thread(void *s_argument) { static timer_t s_timer_id; struct sigevent s_sigevent; struct itimerspec s_itimerspec; sigset_t s_mask; struct sigaction s_sigaction; clockid_t s_clock_id = CLOCK_REALTIME; /* establish handler for timer signal */ s_sigaction.sa_flags = SA_SIGINFO; s_sigaction.sa_sigaction = mztimer_handler; sigemptyset(&s_sigaction.sa_mask); if(sigaction(def_mztimer_realtime_signal, &s_sigaction, NULL) == (-1)) { return(NULL); } /* block timer signal temporarily */ sigemptyset(&s_mask); sigaddset(&s_mask, def_mztimer_realtime_signal); if(sigprocmask(SIG_SETMASK, &s_mask, NULL) == (-1)) { return(NULL); } /* create timer */ s_sigevent.sigev_notify = SIGEV_SIGNAL; s_sigevent.sigev_signo = def_mztimer_realtime_signal; s_sigevent.sigev_value.sival_ptr = &s_timer_id; if(timer_create(s_clock_id, &s_sigevent, &s_timer_id) == (-1)) { return(NULL); } /* start the timer */ s_itimerspec.it_value.tv_sec = g_mztimer_interval_usec / 1000000l; s_itimerspec.it_value.tv_nsec = (g_mztimer_interval_usec % 1000000l) * 1000l; s_itimerspec.it_interval.tv_sec = s_itimerspec.it_value.tv_sec; s_itimerspec.it_interval.tv_nsec = s_itimerspec.it_value.tv_nsec; if(timer_settime(s_timer_id, 0, &s_itimerspec, NULL) == (-1)) { return(NULL); } /* unlock the timer signal */ if(sigprocmask(SIG_UNBLOCK, &s_mask, NULL) == (-1)) { return(NULL); } for(;;) { pause(); } return(NULL); } int mztimer_start(long s_interval_usec) { pthread_t s_thread_handle; sigset_t s_mask; g_mztimer_interval_usec = s_interval_usec; if(pthread_create(&s_thread_handle, NULL, mztimer_thread, NULL) == (-1)) { return(-1); } (void)pthread_detach(s_thread_handle); sigemptyset(&s_mask); sigaddset(&s_mask, def_mztimer_realtime_signal); (void)sigprocmask(SIG_BLOCK, &s_mask, NULL); return(0); } int main(void) { (void)mztimer_start(1000l); for(;;) { sleep(1); (void)fprintf(stdout, "\x1b[1;33msleep loop\x1b[0m\n"); } return(EXIT_SUCCESS); } /* End of source *