'Linux API/linux'에 해당되는 글 61건

  1. 2023.10.19 malloc() 으로 할당된 메모리 리스트?
  2. 2023.06.21 inotify
  3. 2023.01.09 system v shmget size
  4. 2022.11.28 segfault시 calltree 출력하기
  5. 2022.11.24 pthread 테스트
  6. 2022.11.23 pthread
  7. 2022.11.11 iio(industrial io) 문서
  8. 2022.10.26 mkpipe 와 poll
  9. 2022.10.20 ‘F_SETPIPE_SZ’ undeclared
  10. 2022.10.18 linux fifo
Linux API/linux2023. 10. 19. 16:21

관용적으로 malloc() 해서 받은 인자가 0인지 확인하고(fail)

그 값을 포인터에 넣고 썼을 뿐이고, free() 하면서 null 값을 넣어주곤 했는데

반대로 system call이나 라이브러리를 이용하여

현재 사용중인 프로그램에서 malloc()을 요청해서 관리중인 리스트 이런건 구할 수 없는건가?

조사가 필요한 듯..

 

[링크 : https://www.gnu.org/software/libc/manual/html_node/Memory-Allocation-Probes.html]

[링크 : https://www.reddit.com/r/cpp_questions/comments/pi8lhg/how_do_i_check_if_a_pointer_is_valid/]

[링크 : https://stackoverflow.com/questions/993324/how-to-check-if-a-pointer-is-valid]

[링크 : https://stackoverflow.com/questions/5716100/what-happens-in-the-kernel-during-malloc]

'Linux API > linux' 카테고리의 다른 글

/proc/uptime  (0) 2023.10.24
/proc/pid/statm  (0) 2023.10.23
inotify  (0) 2023.06.21
system v shmget size  (0) 2023.01.09
segfault시 calltree 출력하기  (0) 2022.11.28
Posted by 구차니
Linux API/linux2023. 6. 21. 10:37

파일 이벤트를 감시하는 API

 

그런데 내용만 봐서는 디렉토리 레벨로는 어떻게 감시가 되는데

파일시스템 오류로 파티션 전체가 read only로 바뀌면

해당 이벤트가 모니터링 되는진 좀 더 봐야 할 듯.

[링크 : https://man7.org/linux/man-pages/man7/inotify.7.html]

[링크 : https://sonseungha.tistory.com/436]

[링크 : https://dataonair.or.kr/db-tech-reference/d-lounge/technical-data/?mod=document&uid=236732]

'Linux API > linux' 카테고리의 다른 글

/proc/pid/statm  (0) 2023.10.23
malloc() 으로 할당된 메모리 리스트?  (0) 2023.10.19
system v shmget size  (0) 2023.01.09
segfault시 calltree 출력하기  (0) 2022.11.28
pthread 테스트  (0) 2022.11.24
Posted by 구차니
Linux API/linux2023. 1. 9. 19:05

ipcs -m 하면 키, id, size가 나오는데

어떻게 size를 받아내나 싶어서 찾아보니 먼가 나오긴 한다.

 

struct shmid_ds buf;
shmctl(shm, IPC_STAT, &buf);
int length = (int) buf.shm_segsz / sizeof(int);

[링크 : http:// https://stackoverflow.com/questions/60219469/find-size-of-shared-memory-in-c-shmget]

'Linux API > linux' 카테고리의 다른 글

malloc() 으로 할당된 메모리 리스트?  (0) 2023.10.19
inotify  (0) 2023.06.21
segfault시 calltree 출력하기  (0) 2022.11.28
pthread 테스트  (0) 2022.11.24
pthread  (0) 2022.11.23
Posted by 구차니
Linux API/linux2022. 11. 28. 21:29

arm64(i.mx8) 에서는 

caller_address = (void *) uc->uc_mcontext.arm_pc;

대신

caller_address = (void *) uc->uc_mcontext.pc; 로 하니 빌드가 된다.

 

[링크 : https://snowdeer.github.io/c++/2017/08/30/segmentation-fault-call-stack/]

'Linux API > linux' 카테고리의 다른 글

inotify  (0) 2023.06.21
system v shmget size  (0) 2023.01.09
pthread 테스트  (0) 2022.11.24
pthread  (0) 2022.11.23
iio(industrial io) 문서  (0) 2022.11.11
Posted by 구차니
Linux API/linux2022. 11. 24. 10:23

심심(?)해서 pthread를 1회성으로 구동하고 죽게 냅두면 어떻게 되나 궁금해서 해봤는데

메모리 16GB 시스템에서 32753개 정도까지 밖에 구동 못하고 메모리를 할당할 수 없다고 죽는다.

user space 메모리는 문제가 없는 것 같은데 커널 메모리를 다 먹어 버린건가?

$ cat main.c
#include <stdio.h>
#include <pthread.h>

void *func(int *val)
{
        printf("hello %d\n", *val);
}

void main()
{
        pthread_t pt;
        int pid = 0;
        int i = 0;
        for(i = 0; i < 100000; i++)
        {
                pid = pthread_create(&pt, NULL, func, &i);
                if (pid > 0 )
                {
                        perror("thread create error");
                        exit(0);
                }
        }
}
$ gcc main.c -lpthread
$ ./a.out
hello 32753
thread create error: Cannot allocate memory
$ top
top - 10:16:37 up 8 days, 23:07,  3 users,  load average: 0.39, 0.24, 0.09
Tasks: 363 total,   3 running, 293 sleeping,   0 stopped,   0 zombie
%Cpu(s):  6.2 us, 25.0 sy,  0.0 ni, 66.2 id,  0.0 wa,  0.0 hi,  2.5 si,  0.0 st
KiB Mem : 16169232 total,   808336 free,  2131900 used, 13228996 buff/cache
KiB Swap:  2097148 total,  2095356 free,     1792 used. 13436824 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
25250 minimonk   20   0  0.107t 117784   1584 R 131.8  0.7   0:00.29 a.out
22599 minimonk   20   0  110440   6228   4996 R  81.8  0.0   0:17.60 sshd
21729 root      20   0       0      0      0 I   9.1  0.0   0:00.11 kworker/u16:3-e
22764 root      20   0       0      0      0 I   9.1  0.0   0:01.27 kworker/u16:2-e
17413 root      20   0       0      0      0 I   4.5  0.0   0:02.40 kworker/4:0-eve
18086 root      20   0       0      0      0 I   4.5  0.0   0:02.63 kworker/5:0-eve
21979 root      20   0       0      0      0 I   4.5  0.0   0:01.56 kworker/1:1-eve
22094 root      20   0       0      0      0 I   4.5  0.0   0:00.80 kworker/3:1-eve
22446 root      20   0       0      0      0 I   4.5  0.0   0:00.61 kworker/2:0-eve
27802 minimonk   20   0   45652   4232   3476 R   4.5  0.0   0:00.97 top

[링크 : https://www.joinc.co.kr/w/Site/system_programing/Book_LSP/ch07_Thread]

 

 

+

pthread_join()으로 쓰레드가 끝나길 기다리는데 안 끝난다.

return이 없어서인가.. 쓰레드도 종료를 하는 다른 함수가 있는건가?

 

+

다시 해보니 pthread_join에 포인터로 pthread_t 변수를 넘겨서 그랬던 듯. 잘 돈다.

#include <stdio.h>
#include <pthread.h>

void *func(int *val)
{
    printf("hello %d\n", *val);

//  return ((void*)0);
}

void main()
{
    pthread_t pt;
    int pid = 0;
    int i = 0;
    int status = 0;
    for(i = 0; i < 100000; i++)
    {
        pid = pthread_create(&pt, NULL, func, &i);
        if (pid > 0 )
        {
            perror("thread create error");
            exit(0);
        }
        pthread_join(pt, (void**)&status);
    }
}

[링크 : https://www.joinc.co.kr/w/Site/Thread/Advanced/ThreadCancle]

 

좀.. 비슷한 타입으로 하게 하라고 -_-

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);

 

+

pthread_join 안하니 다시 메모리 할당 오류. 쓰레드 에서 return 0 하는거랑은 상관없다.

#include <stdio.h>
#include <pthread.h>

void *func(int *val)
{
        printf("hello %d\n", *val);

//      return ((void*)0);
}

void main()
{
        pthread_t pt;
        int pid = 0;
        int i = 0;
        int status = 0;
        for(i = 0; i < 100000; i++)
        {
                pid = pthread_create(&pt, NULL, func, &i);
                if (pid > 0 )
                {
                        perror("thread create error");
                        exit(0);
                }
//              pthread_join(pt, (void**)&status);
        }
}

 

detach 하면 종료시 자동으로 자원 반납이 된다고 한다.

테스트 해보니 return도 필요없이 쓰레드에서 실행될 함수가 종료되면 그냥 종료된다.

랜덤하게 안되긴 한데, printf가 중첩되서 버퍼가 쌓이며 문제가 생기는거 같기도 하고? (쓰레드를 5만개 넘기는 시점이니)

기본적으로 스레드의 종료 코드는 스레드가 종료되더라도 pthread_join이 호출될 때까지 유지됩니다. 하지만, detatch 상태의 스레드는 종료되는 즉시 자원이 회수됩니다

[링크 : http://xucxo.blogspot.com/2011/03/linux-programming-thread.html]

 

'Linux API > linux' 카테고리의 다른 글

system v shmget size  (0) 2023.01.09
segfault시 calltree 출력하기  (0) 2022.11.28
pthread  (0) 2022.11.23
iio(industrial io) 문서  (0) 2022.11.11
mkpipe 와 poll  (0) 2022.10.26
Posted by 구차니
Linux API/linux2022. 11. 23. 19:03

'Linux API > linux' 카테고리의 다른 글

segfault시 calltree 출력하기  (0) 2022.11.28
pthread 테스트  (0) 2022.11.24
iio(industrial io) 문서  (0) 2022.11.11
mkpipe 와 poll  (0) 2022.10.26
‘F_SETPIPE_SZ’ undeclared  (0) 2022.10.20
Posted by 구차니
Linux API/linux2022. 11. 11. 16:40

내용상 같은 문서 같은데..

왜 kernel.org 문서는 눈에 잘 안들어 올까?

오히려 과도한(?) 서식이 문제인가

 

[링크 : https://www.kernel.org/doc/html/v4.11/driver-api/iio/index.html]

[링크 : https://dbaluta.github.io/index.html]

'Linux API > linux' 카테고리의 다른 글

pthread 테스트  (0) 2022.11.24
pthread  (0) 2022.11.23
mkpipe 와 poll  (0) 2022.10.26
‘F_SETPIPE_SZ’ undeclared  (0) 2022.10.20
linux fifo  (0) 2022.10.18
Posted by 구차니
Linux API/linux2022. 10. 26. 14:24

가능은 한 것 같은데..

[링크 : https://stackoverflow.com/questions/15055065/o-rdwr-on-named-pipes-with-poll]

[링크 : https://man7.org/linux/man-pages/man3/mkfifo.3.html]

 

Macro: int ENXIO“No such device or address.” The system tried to use the device represented by a file you specified, and it couldn’t find the device. This can mean that the device file was installed incorrectly, or that the physical device is missing or not correctly attached to the computer.

[링크 : https://www.gnu.org/software/libc/manual/html_node/Error-Codes.html]

 

+

2022.10.27

POLLIN으로 탐지가 되는 듯?

$ cat rx.c
//#define _GNU_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <linux/fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <sys/poll.h>

#define  FIFO_FILE   "/tmp/fifo"
#define  BUFF_SIZE  1024*768*4
struct pollfd pfd[1];

int main(int argc, char** argv)
{
                int ret = 0;
                int   counter = 0;
                int   fd;
                char  buff[BUFF_SIZE];

                //              printf("argc[%d]\n",argc);
                if ( -1 == mkfifo( FIFO_FILE, 0666)){
                                perror( "mkfifo() 실행에러");
                                exit( 1);
                }

                if ( -1 == ( fd = open( FIFO_FILE, O_RDONLY|O_NONBLOCK))){  //  <---- (A)
                                perror( "open() 실행에러");
                                exit( 1);
                }
                ret = fcntl(fd, F_SETPIPE_SZ, 1024 * 1024);
                pfd[0].fd = fd;
                pfd[0].events = POLLIN;

                printf("fd[%d]ret[%d]\n",fd,ret);

                while( 1 ){
                                int n = poll(pfd, 1, 10);
                                if (n > 0)
                                {
                                        memset( buff, 0, BUFF_SIZE);
                                        ret = read( fd, buff, BUFF_SIZE);
                                        if(ret > 0 )
                                                        printf("ret[%d]\n",ret);
                                        //      printf( "%d: %s\n", counter++, buff);
                                }
                                printf(".");
                                usleep(10000);
                }
                close( fd);
}
$ cat tx.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>

#define  FIFO_FILE   "/tmp/fifo"

int main_loop = 1;
int write_loop = 1;

void sigint_handler(int sig)
{
                main_loop = 0;
                write_loop = 0;
}

void sigpipe_handler(int sig)
{
                write_loop = 0;
}

int main(int argc, char **argv)
{
                int   fd;
                int   fd_r;
                //   char *str   = "badayak.com";
                char *str = NULL;
                char *temp = NULL;

                str = (char*)malloc(1024*768*4);
                temp = (char*)malloc(1024*768*4);
                memset(str, *argv[1], 1024*768*4);

//              signal(SIGPIPE, SIG_IGN);
                signal(SIGPIPE, &sigpipe_handler);
                signal(SIGINT, &sigint_handler);
                /*
                   printf("%d\n",__LINE__);
                   if ( -1 == mkfifo( FIFO_FILE, 0666)){
                   perror( "mkfifo() 실행에러");
                   exit( 1);
                   }
                 */
                while(main_loop)
                {
                                printf("%d\n",__LINE__);
                                do {
                                                fd = open( FIFO_FILE, O_WRONLY | O_NONBLOCK);
                                                usleep(100000);
                                                printf("fd[%d] %d\n",fd,__LINE__);
                                                if(main_loop == 0 && write_loop == 0) exit(1);
                                } while(fd == -1);
                                write_loop = 1;

                                int ret = 0;
                                while(write_loop)
                                {
                                                printf("%d\n",__LINE__);
                                                int a = write( fd, str, atoi(argv[1]));
                                                printf("%d a[%d]\n",__LINE__,a);
                                                usleep(1000000);
                                }
                }
                close( fd);
}

'Linux API > linux' 카테고리의 다른 글

pthread  (0) 2022.11.23
iio(industrial io) 문서  (0) 2022.11.11
‘F_SETPIPE_SZ’ undeclared  (0) 2022.10.20
linux fifo  (0) 2022.10.18
SIGPIPE  (0) 2022.10.17
Posted by 구차니
Linux API/linux2022. 10. 20. 10:50

커널이 5.4.0 이라 그런가

해당 선언을 해주어야 에러가 사라진다.

 

#define _GNU_SOURCE

[링크 : https://stackoverflow.com/questions/25411892/f-setpipe-sz-undeclared]

 

 

Defining _GNU_SOURCE has nothing to do with license and everything to do with writing (non-)portable code. If you define _GNU_SOURCE, you will get:
  1. access to lots of nonstandard GNU/Linux extension functions
  2. access to traditional functions which were omitted from the POSIX standard (often for good reason, such as being replaced with better alternatives, or being tied to particular legacy implementations)
  3. access to low-level functions that cannot be portable, but that you sometimes need for implementing system utilities like mount, ifconfig, etc.
  4. broken behavior for lots of POSIX-specified functions, where the GNU folks disagreed with the standards committee on how the functions should behave and decided to do their own thing.
As long as you're aware of these things, it should not be a problem to define _GNU_SOURCE, but you should avoid defining it and instead define _POSIX_C_SOURCE=200809L or _XOPEN_SOURCE=700 when possible to ensure that your programs are portable.
In particular, the things from _GNU_SOURCE that you should never use are #2 and #4 above.
 

[링크 : https://stackoverflow.com/questions/5582211/what-does-define-gnu-source-imply]

 

호환성 지정 매크로

[링크 : https://m.blog.naver.com/netiz21/150015716721]

 

#include <fcntl.h> 대신

#include <linux/fcntl.h> 하면 _GNU_SOURCE를 해주지 않아도 되긴 한데

다른데서 경고가 뜨니 알아서 써야 할 듯.

/usr/include/linux/fcntl.h:28:#define F_SETPIPE_SZ (F_LINUX_SPECIFIC_BASE + 7)
/usr/include/linux/fcntl.h:29:#define F_GETPIPE_SZ (F_LINUX_SPECIFIC_BASE + 8)

'Linux API > linux' 카테고리의 다른 글

iio(industrial io) 문서  (0) 2022.11.11
mkpipe 와 poll  (0) 2022.10.26
linux fifo  (0) 2022.10.18
SIGPIPE  (0) 2022.10.17
linux ipc 최대 데이터 길이  (0) 2022.10.11
Posted by 구차니
Linux API/linux2022. 10. 18. 18:20

특이하다면 특이하고, 당연하다면 당연하게

fifo는 쓸 녀석과, 읽을 녀석이 둘다 요청이 들어올때 까지 open() 에서 blocking 된다.

 

strace를 이용해서 확인해보면 각각 실행할 경우

O_RDONLY를 주던 O_WRONLY를 주던 간에 open() 함수에서 block 되어있다

두개 프로그램이 read/write pair가 만들어지면 그제서야 open()을 넘어가게 된다.

open()을 non_block 으로 해서 name pipe의 pair가 만들어지길 기다리는 것도 방법 일 듯.

 

$ strace ./rx
openat(AT_FDCWD, "/tmp/fifo", O_RDONLY
$ strace ./tx 2
openat(AT_FDCWD, "/tmp/fifo", O_WRONLY

[링크 : https://tutorialspoint.dev/computer-science/operating-systems/named-pipe-fifo-example-c-program]

 

걍 이렇게 하고 나서 해보면 되려나?

int fifo_fd = open(fifo_path, O_RDONLY | O_NONBLOCK);
FILE *fp = fdopen(fifo_fd, "r");

[링크 : https://cboard.cprogramming.com/c-programming/89358-nonblocking-fifo.html]

'Linux API > linux' 카테고리의 다른 글

mkpipe 와 poll  (0) 2022.10.26
‘F_SETPIPE_SZ’ undeclared  (0) 2022.10.20
SIGPIPE  (0) 2022.10.17
linux ipc 최대 데이터 길이  (0) 2022.10.11
ipc 성능 비교  (0) 2022.09.21
Posted by 구차니