コード例 #1
0
ファイル: mmstress.c プロジェクト: LeqiaoP1/ltp
static void *thread_fault(void *args)
{				/* pointer to the arguments passed to routine */
	long *local_args = args;	/* local pointer to list of arguments        */
	/* local_args[THNUM]   - the thread number   */
	/* local_args[MAPADDR] - map address         */
	/* local_args[PAGESIZ] - page size           */
	/* local_args[FLTIPE]  - fault type          */
	int pgnum_ndx = 0;	/* index to the number of pages              */
	caddr_t start_addr	/* start address of the page                 */
	    = (caddr_t) (local_args[MAPADDR]
			 + (int)local_args[THNUM]
			 * (pages_num / NUMTHREAD)
			 * local_args[PAGESIZ]);
	char read_from_addr = 0;	/* address to which read from page is done   */
	char write_to_addr[] = { 'a' };	/* character to be writen to the page    */
	uintptr_t exit_val = 0;	/* exit value of the pthread. 0 - success    */

    /*************************************************************/
	/*   The way it was, args could be overwritten by subsequent uses
	 *   of it before this routine had a chance to use the data.
	 *   This flag stops the overwrite until this routine gets to
	 *   here.  At this point, it is done initializing and it is
	 *   safe for the parent thread to continue (which will change
	 *   args).
	 */
	thread_begin = FALSE;

	while (wait_thread)
		sched_yield();

	for (; pgnum_ndx < (pages_num / NUMTHREAD); pgnum_ndx++) {
		/* if the fault to be generated is READ_FAULT, read from the page     */
		/* else write a character to the page.                                */
		((int)local_args[3] == READ_FAULT) ? (read_from_addr =
						      *start_addr)
		    : (*start_addr = write_to_addr[0]);
		start_addr += local_args[PAGESIZ];
		if (verbose_print)
			tst_resm(TINFO,
				 "thread_fault(): generating fault type %ld"
				 " @page address %p", local_args[3],
				 start_addr);
		fflush(NULL);
	}
	PTHREAD_EXIT(0);
}
コード例 #2
0
ファイル: shm_test.c プロジェクト: Nan619/ltp
static void *shmat_rd_wr(void *args)
{   /* arguments to the thread function             */
    int shmndx = 0;		/* index to the number of attach and detach   */
    int index = 0;		/* index to the number of blocks touched      */
    int reader = 0;		/* this thread is a reader thread if set to 1 */
    key_t shm_id = 0;	/* shared memory id                           */
    long *locargs =		/* local pointer to arguments                 */
        (long *)args;
    volatile int exit_val = 0;	/* exit value of the pthread                  */
    char *read_from_mem;	/* ptr to touch each (4096) block in memory   */
    char *write_to_mem;	/* ptr to touch each (4096) block in memory   */
    char *shmat_addr;	/* address of the attached memory             */
    char buff;		/* temporary buffer                           */

    reader = (int)locargs[3];
    while (shmndx++ < (int)locargs[0]) {
        dprt("pid[%d]: shmat_rd_wr(): locargs[1] = %#x\n",
             getpid(), (int)locargs[1]);

        /* get shared memory id */
        if ((shm_id =
                    shmget((int)locargs[1], (int)locargs[2], IPC_CREAT | 0666))
                == -1) {
            dprt("pid[%d]: shmat_rd_wr(): shmget failed\n",
                 getpid());
            perror("do_shmat_shmadt(): shmget()");
            PTHREAD_EXIT(-1);
        }

        fprintf(stdout, "pid[%d]: shmat_rd_wr(): shmget():"
                "success got segment id %d\n", getpid(), shm_id);

        /* get shared memory segment */
        if ((shmat_addr = shmat(shm_id, NULL, 0)) == (void *)-1) {
            rm_shared_mem(shm_id, shmat_addr, 0);
            fprintf(stderr,
                    "pid[%d]: do_shmat_shmadt(): shmat_addr = %#lx\n",
                    getpid(), (long)shmat_addr);
            perror("do_shmat_shmadt(): shmat()");
            PTHREAD_EXIT(-1);
        }
        dprt("pid[%d]: do_shmat_shmadt(): content of memory shmat_addr = %s\n", getpid(), shmat_addr);

        fprintf(stdout,
                "pid[%d]: do_shmat_shmadt(): got shmat address = %#lx\n",
                getpid(), (long)shmat_addr);

        if (!reader) {
            /* write character 'Y' to that memory area */
            index = 0;
            write_to_mem = shmat_addr;
            while (index < (int)locargs[2]) {
                dprt("pid[%d]: do_shmat_shmatd(): write_to_mem = %#x\n", getpid(), write_to_mem);
                *write_to_mem = 'Y';
                index++;
                write_to_mem++;
                sched_yield();
            }
        } else {
            /* read from the memory area */
            index = 0;
            read_from_mem = shmat_addr;
            while (index < (int)locargs[2]) {
                buff = *read_from_mem;
                index++;
                read_from_mem++;
                sched_yield();
            }
        }

        sched_yield();

        /* remove the shared memory */
        if (rm_shared_mem(shm_id, shmat_addr, 1) == -1) {
            fprintf(stderr,
                    "pid[%d]: do_shmat_shmatd(): rm_shared_mem(): faild to rm id\n",
                    getpid());
            PTHREAD_EXIT(-1);
        }
    }

    PTHREAD_EXIT(0);
}
コード例 #3
0
ファイル: make_tree.c プロジェクト: Mellanox/arc_ltp
static void *
crte_mk_rm(void *args)
{
    int 	dircnt;		/* index to the number of subdirectories      */
    int		fd;		/* file discriptor of the files genetated     */
    int		filecnt;	/* index to the number of ".c" files created  */
    int		numchar[2];	/* number of characters written to buffer     */
    char 	*dirname;	/* name of the directory/idirectory tree      */
    char 	*tmpdirname;	/* name of a temporary directory, for swaping */
    char	*cfilename;     /* name of the ".c" file created	      */
    char	*mkfilename;	/* name of the makefile - which is "makefile" */
    char	*hostname;	/* hostname of the client machine             */
    char	*prog_buf;	/* buffer containing contents of the ".c" file*/
    char	*make_buf;	/* buffer the contents of the makefile        */
    char	*pwd;	        /* contains the current working directory     */
    long	*locargptr =	/* local pointer to arguments                 */
                             (long *)args;
    volatile int exit_val = 0;  /* exit value of the pthreads		      */

    if ((dirname = malloc(sizeof(char) * 2048)) == NULL) /* just paranoid */
    {
        perror("crte_mk_rm(): dirname malloc()");
	PTHREAD_EXIT(-1);
    }

    if ((tmpdirname = malloc(sizeof(char) * 2048)) == NULL)
    {
        perror("crte_mk_rm(): tmpdirname malloc()");
	PTHREAD_EXIT(-1);
    }

    if ((cfilename = malloc(sizeof(char) * 2048)) == NULL)
    {
        perror("crte_mk_rm(): cfilename malloc()");
	PTHREAD_EXIT(-1);
    }

    if ((mkfilename = malloc(sizeof(char) * 2048)) == NULL)
    {
        perror("crte_mk_rm(): mkfilename malloc()");
	PTHREAD_EXIT(-1);
    }

    if ((prog_buf = malloc(sizeof(char) * 4096)) == NULL)
    {
        perror("crte_mk_rm(): prog_buf malloc()");
	PTHREAD_EXIT(-1);
    }

    if ((pwd = malloc(PATH_MAX)) == NULL)
    {
        perror("crte_mk_rm(): pwd malloc()");
	PTHREAD_EXIT(-1);
    }

    if ((hostname = malloc(sizeof(char) * 1024)) == NULL)
    {
        perror("crte_mk_rm(): hostname malloc()");
	PTHREAD_EXIT(-1);
    }

    if (gethostname(hostname, 255) == -1)
    {
        perror("crte_mk_rm(): gethostname()");
        PTHREAD_EXIT(-1);
    }
    if (!getcwd(pwd, PATH_MAX))
    {
        perror("crte_mk_rm(): getcwd()");
	PTHREAD_EXIT(-1);
    }

    numchar[0] = sprintf(prog_buf,
                "main()\n{\n\t printf(\"hello world\");\n}\n");

    for (dircnt = 0; dircnt < (int)locargptr[0]; dircnt++)
    {
        /* First create the base directory, then create the subdirectories   */
        if (dircnt == 0)
            sprintf(dirname, "%s.%ld", hostname, gettid());
        else
        {
            sprintf(tmpdirname, "%s/%ld.%d", dirname, gettid(), dircnt);
            sprintf(dirname, "%s", tmpdirname);
        }
        sync();

        dprt("pid[%d] creating directory: %s\n", gettid(), dirname);
        if (mkdir(dirname, 0777) == -1)
        {
            perror("crte_mk_rm(): mkdir()");
	    PTHREAD_EXIT(-1);
        }
    }

    sync();
    usleep(10);
    for (dircnt = 0; dircnt < (int)locargptr[0]; dircnt++)
    {
        if (dircnt == 0)
            sprintf(dirname, "%s/%s.%ld", pwd, hostname, gettid());
        else
        {
            sprintf(tmpdirname, "%s/%ld.%d", dirname, gettid(), dircnt);
            sprintf(dirname, "%s", tmpdirname);
        }
        sync();
        if ((make_buf = malloc(sizeof(char) * 4096)) == NULL)
        {
            perror("crte_mk_rm(): make_buf malloc()");
            PTHREAD_EXIT(-1);
        }
        sprintf(mkfilename, "%s/makefile", dirname);
        {
            /* HACK! I could not write "%.c" to the makefile */
            /* there is probably a correct way to do it      */
            char *dotc = malloc(10);
            dotc = ".c";
            sync();
            usleep(10);
	    if (dircnt == (locargptr[0] - 1))
            {
                numchar[1] = sprintf(make_buf,
                "CFLAGS := -O -w -g\n"
                "SUBDIRS = %ld.%d\n"
                "SRCS=$(wildcard *.c)\n"
                "TARGETS=$(patsubst %%%s,\%%,$(SRCS))\n"
                "all:\t $(TARGETS)\n"
                "clean:\n"
                "\trm -f $(TARGETS)\n",
                       gettid(), dircnt + 1, dotc);
	    }
            else
            {
	        numchar[1] = sprintf(make_buf,
	        "CFLAGS := -O -w -g\n"
	        "SUBDIRS = %ld.%d\n"
	        "SRCS=$(wildcard *.c)\n"
	        "TARGETS=$(patsubst %%%s,\%%,$(SRCS))\n\n\n"
	        "all:\t $(TARGETS)\n"
                "\t@for i in $(SUBDIRS); do $(MAKE) -C $$i ; done\n\n"
	        "clean:\n"
	        "\trm -f $(TARGETS)\n"
                "\t@for i in $(SUBDIRS); do $(MAKE) -C $$i clean ; done\n",
                       gettid(), dircnt + 1, dotc);
            }
        }