在外企做服务器开发, 目前是项目经理, 管理两个server开发的项目。不做嵌入式好久了。
全部博文(197)
分类: LINUX
2007-09-28 12:54:08
|
|
/* By process name , you can get task_struct for which wake up ,bob added */ int self_find_task_by_name(char *ps_name) { pid_t pid = -1; // int counter = 0; struct task_struct *p = current->next_task; struct task_struct *p_query_head = current ; //loop times depends on the number of the process in current system while(p != NULL && p->pid != p_query_head->pid) { if( ! memcmp(p->comm,ps_name,strlen(ps_name))) { pid = p->pid; goto p_find; } p=p->next_task; // counter++ ; } dbg("Can't find the process named %s,you should run it in advance\n",ps_name); return -1; p_find: return pid; } |
By process name , you can get task_struct for which wake up ,bob added */ int self_find_task_by_name(char *ps_name) { pid_t pid = -1; struct list_head *p = NULL; struct list_head *head = ¤t->tasks; list_for_each(p,head) { #define this_task(p) list_entry(p,struct task_struct, tasks) struct task_struct *task = NULL; task = this_task(p); dbg("this process name = %s\n",task->comm); if(unlikely(!memcmp(task->comm,ps_name,strlen(ps_name)))) { pid = task->pid; goto p_find; } } dbg("Can't find the process named %s,you should run it in advance\n",ps_name); return -1; p_find: return pid; } |
#define this_job(p) list_entry(p, struct bob_jobs, list) static __init int chardev_init(void) { char *p_name = "smbd"; struct list_head *p = NULL; //begin struct bob_jobs job1 = { .id = 10UL, //.list = LIST_HEAD_INIT(list), 因为我们的结构体刚定义,list指针本身就是未知的,需要在list_add()才能对其进行操作,不用没法赋值。 }; struct bob_jobs job2 = { .id = 20UL, //.list = LIST_HEAD_INIT(list), 因为我们的结构体刚定义,list指针本身就是未知的,需要在list_add()才能对其进行操作,不用没法赋值。 }; Major = register_chrdev(0, DEVICE_NAME, &fops); if (Major < 0) { dbg("Registering the character device failed with %d\n", Major); return Major; } dbg("I was assigned major number %d. To talk to\n", Major); dbg("the driver, create a dev file with\n"); dbg("'mknod /dev/hello c %d 0'.\n", Major); dbg("Try various minor numbers. Try to cat and echo to\n"); dbg("the device file.\n"); dbg("Remove the device file and module when done.\n"); //check the self_find_by_name() function dbg("will call self_find_by_name() \n"); dbg("smbd pid=%d\n",self_find_task_by_name("smbd")); //only debug the functions in list.h dbg("only debug the functions in list.h\n"); INIT_LIST_HEAD(&bob_list); //initialize the double list list_add(&(job1.list),&bob_list); list_add(&(job2.list),&bob_list); list_for_each(p,&bob_list) { struct bob_jobs *job = NULL; job = this_job(p); dbg("id=%lu\n",job->id); } dbg("will delete the bob_list double list\n"); while(bob_list.next != &bob_list) { struct bob_jobs *job = NULL; p = bob_list.next; job = this_job(p); //delete a entry after header // dbg("delete a entry after header , id = %lu\n",(this_job(p))->id); dbg("delete a entry after header , id = %lu\n",job->id); list_del(bob_list.next); } dbg("delete over \n"); return 0; } |
/* 用信号量来保护临界区, 而且, 一次只能让一个进程管道的数据读走 */ static ssize_t device_read(struct file *filp, /* see include/linux/fs.h */ char *buffer, /* buffer to fill with data */ size_t length, /* length of the buffer */ loff_t *offset) /* loff_t == long long */ { // Number of bytes actually written to the buffer // DECLARE_WAITQUEUE(wait, current); //int bytes_read = 0; int ret = 0; char *read_msg_ptr = msg; /* 如果msg[0] == '\0',说明无数据可读,就睡眠 */ dbg("into device_read() function \n"); // add_wait_queue(&read_wait, &wait);//this is versersaile // // do { // //这些唤醒的进程当中,肯定有一个最先获得信号量 // down_interruptible(&char_sem); //获取信号量,实现独占 // __set_current_state(TASK_INTERRUPTIBLE); // // if (msg[0] != '\0'){ //have data to read // ret = 0; // break; // } // // if (filp->f_flags & O_NONBLOCK) { // dbg("app has set O_NONBLOCK flag \n"); // ret = -EAGAIN; // break; // } // if (signal_pending(current)) { // ret = -ERESTARTSYS; // break; // } // up(&char_sem); // schedule(); // } while (1); // // // set_current_state(TASK_RUNNING); // remove_wait_queue(&read_wait, &wait); //另外一种写法,直接调用wait_event_interruptible() 函数 if (filp->f_flags & O_NONBLOCK) { dbg("app has set O_NONBLOCK \n"); ret = -EAGAIN; return ret; } //直接调用wait_event_interruptible() 函数, 会节省很多代码行数, if(wait_event_interruptible(read_wait, msg[0] != '\0')) return -ERESTARTSYS; if (ret == 0) //正确的情况,而不是下面的两种错误情况 { #if 0 //我写成一个函数了:put_user_string while (length && *read_msg_ptr) { /* * The buffer is in the user data segment, not the kernel * segment so "*" assignment won't work. We have to use * put_user which copies data from the kernel data segment to * the user data segment. */ // kernel ----> user put_user(*(read_msg_ptr++), buffer++); length--; bytes_read++; } /* copy_to_user() 具体怎么用呢? */ // copy_to_user(buffer,msg_Ptr,length); #endif //use my function instead of these code ret = put_user_string(read_msg_ptr,buffer,length); memset(msg,'\0',sizeof(msg)); //彻底清空里面的内容,以此表示pipe的内容只可以读一次,然后就没有了。 } up(&char_sem); return ret; } |