亂貼小站公告欄

目前日期文章:201111 (4)

瀏覽方式: 標題列表 簡短摘要
/*
    gcc -o a a.c -pthread
    ./a && ./a && ./a && ./a && ./a && ./a && ./a && ./a && ./a && ./a
*/
#include <semaphore.h>
#include <pthread.h>
#include <stdio.h>
int count=0;
void inc(void){
        int i=0;
        for(i=0;i<25000000;i++){
                count++;
        }
        pthread_exit(NULL);
}
void dec(void){
        int i=0;
        for(i=0;i<25000000;i++){
                count--;
        }
        pthread_exit(NULL);
}
int main(void){
        int i=0;
        pthread_t id[4];        
        pthread_create(&id[0],NULL,(void*)dec,NULL);
    pthread_create(&id[1],NULL,(void*)inc,NULL);
    pthread_create(&id[2],NULL,(void*)dec,NULL);
    pthread_create(&id[3],NULL,(void*)inc,NULL);
    for(i=0;i<4;i++)
    {
                pthread_join(id[i],NULL);       
        }
        printf("\noutput is %d\n",count);
}

 

/*
    gcc -o a a.c -pthread
    ./a && ./a && ./a && ./a && ./a && ./a && ./a && ./a && ./a && ./a
*/
#include <semaphore.h>
#include <pthread.h>
#include <stdio.h>
int count=0;
sem_t sem;

void inc(void){
        int i=0;
        for(i=0;i<25000000;i++){
        sem_wait(&sem);
                count++;
        sem_post(&sem);
        }
        pthread_exit(NULL);
}
void dec(void){
        int i=0;
        for(i=0;i<25000000;i++){
        sem_wait(&sem);
                count--;
        sem_post(&sem);
        }
        pthread_exit(NULL);
}
int main(void){
    sem_init(&sem,0,1);
        int i=0;
        pthread_t id[4];        
        pthread_create(&id[0],NULL,(void*)dec,NULL);
    pthread_create(&id[1],NULL,(void*)inc,NULL);
    pthread_create(&id[2],NULL,(void*)dec,NULL);
    pthread_create(&id[3],NULL,(void*)inc,NULL);
    for(i=0;i<4;i++)
    {
                pthread_join(id[i],NULL);       
        }
        printf("\noutput is %d\n",count);
    sem_destroy(&sem);
    return 0;
}

 

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include </usr/include/semaphore.h>

#define BUFF_SIZE   5           /* total number of slots */
#define NP          3           /* total number of producers */
#define NC          3           /* total number of consumers */
#define NITERS      4           /* number of items produced/consumed */

typedef struct {
    int buf[BUFF_SIZE];   /* shared var */
    int in;               /* buf[in%BUFF_SIZE] is the first empty slot */
    int out;              /* buf[out%BUFF_SIZE] is the first full slot */
    sem_t full;           /* keep track of the number of full spots */
    sem_t empty;          /* keep track of the number of empty spots */
    sem_t mutex;          /* enforce mutual exclusion to shared data */
} sbuf_t;

sbuf_t shared;

void *Producer(void *arg)
{
    int i, item, index;

    index = (int)arg;

    for (i=0; i < NITERS; i++) {

        /* Produce item */
        item = i;       

        /* Prepare to write item to buf */

        /* If there are no empty slots, wait */
        sem_wait(&shared.empty);
        /* If another thread uses the buffer, wait */
        sem_wait(&shared.mutex);
        shared.buf[shared.in] = item;
        shared.in = (shared.in+1)%BUFF_SIZE;
        printf("[P%d] Producing %d ...\n", index, item); fflush(stdout);
        /* Release the buffer */
        sem_post(&shared.mutex);
        /* Increment the number of full slots */
        sem_post(&shared.full);

        /* Interleave  producer and consumer execution */
        if (i % 2 == 1) sleep(1);
    }
    return NULL;
}

void *Consumer(void *arg)
{
    /* Fill in the code here */
    int i, item, index;

    index = (int)arg;

    for (i=0; i < NITERS; i++) {
        /* Prepare to read item from buf */

        /* If there are no full spots, wait */
        sem_wait(&shared.full);
        /* If another thread uses the buffer, wait */
        sem_wait(&shared.mutex);
        
        /* Consume item */
        item = shared.buf[shared.out];
        shared.out = (shared.out+1)%BUFF_SIZE;
        printf(" ------> [P%d] Consuming %d ...\n", index, item); fflush(stdout);
        /* Release the buffer */
        sem_post(&shared.mutex);
        /* Increment the number of full slots */
        sem_post(&shared.empty);

        /* Interleave  producer and consumer execution */
        if (i % 2 == 1) sleep(1);
    }
    return NULL;
}

int main()
{
    pthread_t idP, idC;
    int index;

    sem_init(&shared.full, 0, 0);
    sem_init(&shared.empty, 0, BUFF_SIZE);

    /* Insert code here to initialize mutex*/
    sem_init(&shared.mutex, 0, 1);

    for (index = 0; index < NP; index++)
    {  
       /* Create a new producer */
       pthread_create(&idP, NULL, Producer, (void*)index);
    }

    /* Insert code here to create NC consumers */
    for (index = 0; index < NC; index++)
    {  
       /* Create a new producer */
       pthread_create(&idC, NULL, Consumer, (void*)index);
    }

    pthread_exit(NULL);
}

和風信使 發表在 痞客邦 PIXNET 留言(0) 人氣()

/*
Step1: Using the fork() function to create a new process.

Step2: Using command line to kill the process.
        kill -kill 1899

Step3: Repeat step1 and use system calls to kill process.
        
*/
#include<stdio.h>
#include<unistd.h>

int main()
{
    pid_t newpid = fork();
    if(newpid==0)
    {
        for(;;)
            pause();
    }
    printf("pid = %d\n",newpid);
    wait(NULL);
    printf("Child process is finished.\n");
}

 

#include<stdio.h>
#include<unistd.h>
#include<signal.h>

int main()
{
    pid_t newpid;
    printf("Input the pid of the process you want to kill -> ");
    scanf("%d",&newpid);
    kill(newpid,SIGKILL);
}

 

#include<stdio.h>
#include<unistd.h>
#include<signal.h>

struct sigaction newact;

/* here is the signal handler */ 
void catch_alarm_2(int sig_num)   /* the argument is signal number  */
{   
    /* re-set the signal handler again to catch_int, for next time */
    sigaction(SIGINT, &newact, NULL);
}
void catch_int_1(int sig_num)   /* the argument is signal number  */
{   
    /* re-set the signal handler again to catch_int, for next time */
    signal(SIGINT, catch_int_1);
    printf("How are you?\n",sig_num);
}
void catch_int_2(int sig_num)   /* the argument is signal number  */
{   
    /* re-set the signal handler again to catch_int, for next time */
    signal(SIGINT, catch_int_2);
    printf("I am fine!\n",sig_num);
}
void catch_alarm_1(int sig_num)   /* the argument is signal number  */
{   
    /* re-set the signal handler again to catch_int, for next time */
    signal(SIGINT, catch_int_2);
    signal(SIGALRM, catch_alarm_2);
    alarm(3);
}

int main(int argc, char* argv[])
{
    sigaction(SIGINT, NULL, &newact);
    
    /* set the INT (Ctrl-C) signal handler to 'catch_int' */
    signal(SIGINT, catch_int_1);
    signal(SIGALRM, catch_alarm_1);
    alarm(3);

    /* now, lets get into an infinite loop of doing nothing. */
    for ( ;; )
        pause();
} 

和風信使 發表在 痞客邦 PIXNET 留言(0) 人氣()

/*
Compile Command:
    gcc -o a a.c -pthread
*/
#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>

void *PrintHello(void *arg)
{
    printf("This is Hello Thread\n");
    printf("Thread ID:%lu\n",pthread_self());
    printf("Argument: %d\n",(int)arg);
    //printf("Argument: %d\n",*(int*)arg);
    pthread_exit(NULL);
}
 
int main()
{
    pthread_t thread;
    int rc,t=100;
    rc = pthread_create( &thread, NULL, PrintHello, (void*)t);
    if(rc)
    {
        printf("ERROR: return code from pthread_create() is %d\n",rc);
        exit(-1);
    }
    rc = pthread_join( thread, NULL);
    if(rc)
    {
        printf("ERROR: return code from pthread_join() is %d\n",rc);
        exit(-1);
    }
    return 0;
}

 

/*
Compile Command:
    gcc -o b b.c -pthread
 
Questions:
#   Observe all of the results you got, and think about what
    problem does it have? (10pts.)
#   Compare to fork() process by doing all the same thing to
    the global variable, can you figure out what’s the
    difference between them? (10pts.)
 
*/
#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>
 
int count = 0;
 
void *PrintHello(void *arg)
{
    int ia=250000;
    //printf("%d) Start: count = %d\n",(int)arg, count);
    while(ia--)
    {
        count++;
        //usleep(1);
    }
    //printf("%d) End: count = %d\n",(int)arg, count);
    printf("Thread %d: ID %lu Completed.\n",(int)arg,pthread_self());
    pthread_exit(NULL);
}
 
int main()
{
    pthread_t thread[4];
    int rc,t;
    for(t=0;t<4;t++)
    {
        rc = pthread_create( &thread[t], NULL, PrintHello, (void*)t);
        if(rc)
        {
            printf("ERROR: return code from pthread_create() is %d\n",rc);
            exit(-1);
        }
    }
    for(t=0;t<4;t++)
    {
        rc = pthread_join( thread[t], NULL);
        if(rc)
        {
            printf("ERROR: return code from pthread_join() is %d\n",rc);
            exit(-1);
        }
    }
    printf("Value = %d\n",count);
    return 0;
}

 

/*
Compile Command:
    gcc -o c c.c -pthread
 
*/
#include<pthread.h>
#include<unistd.h>
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<stdlib.h>
#include "msg_que.h"
 
long int ProgramID = 1; // 1 or 2
int closeMsgQue = 0;
 
void *MsgSend(void *arg)
{
    key_t key=0x1234;
        int msqid = msgget(key,IPC_CREAT | 0666);
        oslab_msg_st msg_buffer;
        while( printf("Enter some text: "),fgets(msg_buffer.msg_text,256,stdin))
        {
                msg_buffer.oslab_msg_type = ProgramID;
                msgsnd(msqid,(void*)&msg_buffer,sizeof(msg_buffer)-sizeof(long int),0);
                if(strcmp(msg_buffer.msg_text,"exit\n")==0)
                {
            closeMsgQue = 1;
                        break;
        }
        }
    pthread_exit(NULL);
}
void *MsgGet(void *arg)
{
    key_t key=0x1234;
        int msqid = msgget(key,IPC_CREAT | 0666);
        oslab_msg_st msg_buffer;
        struct msqid_ds msq_id;
        msgctl(msqid,IPC_STAT,&msq_id);
        while( msgrcv(msqid,(void*)&msg_buffer,sizeof(msg_buffer)-sizeof(long int),3-ProgramID,0) )
        {
                printf("Received: %s\n",msg_buffer.msg_text);
                if(strcmp(msg_buffer.msg_text,"exit\n")==0)
                {
                        if(closeMsgQue)msgctl(msqid,IPC_RMID,&msq_id);
                        break;
                }
        }
    pthread_exit(NULL);
}
 
int main()
{
    pthread_t thread[2];
    int rc,t;
    rc = pthread_create( &thread[0], NULL, MsgSend, (void*)t);
    if(rc)
    {
        printf("ERROR: return code from pthread_create() is %d\n",rc);
        exit(-1);
    }
    rc = pthread_create( &thread[1], NULL, MsgGet, (void*)t);
    if(rc)
    {
        printf("ERROR: return code from pthread_create() is %d\n",rc);
        exit(-1);
    }
    rc = pthread_join( thread[0], NULL);
    if(rc)
    {
        printf("ERROR: return code from pthread_join() is %d\n",rc);
        exit(-1);
    }
    rc = pthread_join( thread[1], NULL);
    if(rc)
    {
        printf("ERROR: return code from pthread_join() is %d\n",rc);
        exit(-1);
    }
    return 0;
}

和風信使 發表在 痞客邦 PIXNET 留言(1) 人氣()

不知道從何時開始

我想要用 webATM 時,

總是會困擾於 ActiveX 不會提示的問題

現在 我終於找的一個解決辦法了

雖然說我覺得這個解決辦法不是治本之道

我找到的這個方法是 重設IE9

http://support.microsoft.com/kb/923737/zh-tw#fixit4me

目前我試了,的確是有用:)


和風信使 發表在 痞客邦 PIXNET 留言(1) 人氣()