



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);
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_time_local = mktime((struct tm *)(&s_set_tm_local)); /* struct
tm구조체형식을 time_t 형식으로 변환 */
if(stime((time_t *)(&s_set_time_local)) == 0) {
/* 설정완료 */
}
...
#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 *