{"id":290441,"date":"2024-03-21T05:24:58","date_gmt":"2024-03-20T21:24:58","guid":{"rendered":"https:\/\/gulass.cn\/dhulghsygjs.html"},"modified":"2024-03-21T05:24:58","modified_gmt":"2024-03-20T21:24:58","slug":"dhulghsygjs","status":"publish","type":"post","link":"https:\/\/gulass.cn\/dhulghsygjs.html","title":{"rendered":"down\u548cup\u4e24\u4e2a\u51fd\u6570\u4e00\u4e2a\u52a0\u9501"},"content":{"rendered":"
linux\u5185\u6838\u8baf\u53f7\u91cf\u81ea\u5b9e\u73b0<\/p>\n
\u4ecb\u7ecd<\/p>\n
\u5927\u7ea6\u5c31\u662fdown\u548cup\u4e24\u4e2a\u51fd\u6570<\/p>\n
\u4e00\u4e2a\u52a0\u9501linux\u5185\u6838\u4fe1\u53f7\u91cf<\/strong>\uff0c\u4e00\u4e2a\u89e3\u9501<\/p>\n \u8fd9\u513f\u4e3b\u8981\u7528\u4e8e\u8fdb\u7a0b\u540c\u6b65<\/p>\n \u6838\u5fc3api<\/p>\n set_current_state<\/p>\n schedule<\/p>\n wake_up_process<\/p>\n linux\u6e90\u7801\u5b9e\u73b0<\/p>\n <\/p>\n \u5728linux\u5185\u6838\u6e90\u7801\u91cc\u5bf9\u4e8e\u8baf\u53f7\u91cf\u8d44\u6e90\u64cd\u4f5c\uff0c\u4e3b\u8981\u662f\u901a\u8fc7\u8f7d\u6d41\u5b50\u9501<\/p>\n \u7ed3\u6784\u4f53\u5927\u7ea6\u5982\u4e0b\uff1a<\/p>\n \n \u5b9e\u73b0\u539f\u7406<\/p>\n <\/p>\n task_struct\u8fd9\u4e2a\u7ed3\u6784\u4f53\u50a8\u5b58\u4e86\u8fdb\u7a0bcontext\uff0c\u987b\u8981\u53d8\u6210\u6570\u7ec4\u65b9\u5f0f\uff0c\u4fbf\u6377\u7761\u7720\u65f6\u50a8\u5b58linux\u5185\u6838\u4fe1\u53f7\u91cf<\/strong>\uff0c\u4ee5\u53ca\u5524\u8d77\u65f6\u4f7f\u7528<\/p>\n \u5927\u7ea6\u6d41\u7a0b\u5982\u4e0b\uff1a<\/p>\n down\u91cc\u52a0\u5165\u8fdb\u7a0b\u5230\u7b49\u5f85\u94fe\uff0c\u4e4b\u540e\u8c03\u7528set_current_state\u548cschedule\u7761\u7720<\/p>\n up\u91cc\u53d6\u51fa\u7b49\u5f85\u94fe\u4e2d\u7684\u8fdb\u7a0b\uff0c\u518d\u8c03\u7528wake_up_process\u5524\u8d77<\/p>\n <\/p>\n \u81ea\u5b9e\u73b0<\/p>\n \u7ed3\u6784\u5b9a\u4e49<\/p>\n \n \u8fd9\u513f\u7528\u4e86\u9996\u5c3e\u4e24\u4e2a\u7b49\u5f85\u94felinux\u547d\u4ee4vi\uff0c\u4fbf\u6377\u64cd\u4f5c<\/p>\n \u2026<\/p>\n down\u5b9e\u73b0<\/p>\n \n up\u5b9e\u73b0<\/p>\n \n ps:\u6211\u8fd9\u6ca1\u6709\u7528\u8f7d\u6d41\u5b50\u9501\u7ea2\u8054linux\u8bba\u575b\uff0c\u800c\u662f\u7528\u7684\u4e92\u65a5\uff0c\u4f3c\u4e4e\u90fd\u5dee\u4e0d\u591a\uff0c\u4e92\u65a5\u4fbf\u6377\u4e9b\uff0c\u9047\u89c1\u5e76\u53d1\u65e0\u8111\u4e92\u65a5\u5c31\u5bf9\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":" linux\u5185\u6838\u4fe1\u53f7\u91cf\u81ea\u5b9e\u73b0linux\u6e90\u7801\u5b9e\u73b0\u5728linux\u5185\u6838\u6e90\u7801\u91cc\u5bf9\u4e8e\u4fe1\u53f7\u91cf\u8d44\u6e90\u64cd\u4f5c\uff0c\u4e3b\u8981\u662f\u901a\u8fc7\u81ea\u65cb\u9501task_struct\u8fd9\u4e2a\u7ed3\u6784\u4f53\u5b58\u50a8\u4e86\u8fdb\u7a0bcontext\uff0c\u9700\u8981\u5f04\u6210\u94fe\u8868\u5f62\u5f0f\uff0c\u65b9\u4fbf\u7761\u7720\u65f6\u5b58\u50a8\uff0c\u4ee5\u53ca\u5524\u9192\u65f6\u4f7f\u7528<\/p>\n","protected":false},"author":1,"featured_media":290442,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[988],"tags":[999],"class_list":["post-290441","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tougao","tag-999"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/290441","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=290441"}],"version-history":[{"count":0,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/290441\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/290442"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=290441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=290441"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=290441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}struct sem\n{\n\tint rc;\n\tsipnlock lock;\n\tstruct task_struct* queue_list;\n}\n<\/code><\/pre>\n<\/p>\n
struct task_queue\n{\n\t\/\/process task\n\tstruct task_struct* task;\n\t\/\/next process task\n\tstruct task_queue* next;\n};\nstruct mysem\n{\n int value;\n\t\/\/mutex lock pointer\n struct mutex* sem_mutex;\n\t\/\/head task queue\n struct task_queue* task_queue_head;\n\t\/\/tail task queue\n\tstruct task_queue* task_queue_tail;\n};\n<\/code><\/pre>\n<\/p>\n
SYSCALL_DEFINE1(down, struct mysem __user* ,sem)\n{\n if(sem == NULL)\n {\n printk(\"sem is nulln\");\n return -1;\n }\n\t\n\t\/\/Judge whether the sem_mutex is NULL\n\tif(sem->sem_mutex == NULL)\n {\n\t\t\/\/define the sem_mutex\n\t\tDEFINE_MUTEX(sem_mutex);\n\t\t\/\/to assign memory for the sem_mutex\n\t\tsem->sem_mutex = kmalloc(sizeof(struct mutex), GFP_KERNEL);\n\t\tif(sem->sem_mutex)\n\t\t{\n\t\t\t\/\/set sem_mutex to sem->sem_mutex\n\t\t\t*(sem->sem_mutex) = sem_mutex;\n\t\t\t\/\/resources--\n\t\t\tsem->value--;\n\t\t\treturn 0;\n\t\t}\n\t\telse\n {\n printk(\"kmalloc sem_mutex failedn\");\n return -1;\n }\n\t}\n\t\/\/lock\n\tmutex_lock(sem->sem_mutex);\n\t\n\t\/\/loop judging\n\twhile(sem->value task_queue_head == NULL)\n\t\t{\n\t\t\t\/\/to assign memory for the task_queue_head\n\t\t\tsem->task_queue_head = kmalloc(sizeof(struct task_queue), GFP_KERNEL);\n\t\t\tif(sem->task_queue_head == NULL)\n\t\t\t{\n\t\t\t\tprintk(\"kmalloc ln errorn\");\n\t\t\t\tmutex_unlock(sem->sem_mutex);\n\t\t\t\treturn -1;\n\t\t\t}\n\t\t\t\n\t\t\t\/\/set current to task_list->ts\n\t\t\tsem->task_queue_head->task = current;\n\t\t\tsem->task_queue_head->next = NULL;\n\t\t\t\/\/set task_queue_head to task_queue_tail\n sem->task_queue_tail = sem->task_queue_head;\n\t\t}\n\t\telse\n\t\t{\n\t\t\t\n\t\t\t\/\/to assign memory for the sem->task_queue_tail->next \n\t\t\tsem->task_queue_tail->next = kmalloc(sizeof(struct task_queue), GFP_KERNEL);\n\t\t\tif(sem->task_queue_tail->next == NULL)\n\t\t\t{\n\t\t\t\tprintk(\"kmalloc ln errorn\");\n\t\t\t\tmutex_unlock(sem->sem_mutex);\n\t\t\t\treturn -1;\n\t\t\t}\n\t\t\t\n\t\t\t\/\/set current to sem->task_queue_tail->next->task\n\t\t\tsem->task_queue_tail->next->task = current;\n\t\t\tsem->task_queue_tail->next->next = NULL;\n\t\t}\n\t\t\t\n\t\t\/\/set state\n\t\tset_current_state(TASK_INTERRUPTIBLE);\n\t\t\/\/unlock\n\t\tmutex_unlock(sem->sem_mutex);\n\t\tschedule();\n\t\t\/\/lock\n\t\tmutex_lock(sem->sem_mutex);\n\t}\n\t\/\/resource--\n sem->value--;\n\t\/\/unlock\n\tmutex_unlock(sem->sem_mutex);\n return 0;\n}\n<\/code><\/pre>\n<\/p>\n
SYSCALL_DEFINE1(up, struct mysem __user* ,sem)\n{\n\tstruct task_queue* temp = NULL;\n\t\n if(sem == NULL)\n {\n printk(\"sem is nulln\");\n return -1;\n }\n\t\n\t\/\/lock\n mutex_lock(sem->sem_mutex);\n\t\n\ttemp = sem->task_queue_head;\n\t\n\t\/\/resources--\n sem->value++;\n\t\n\t\/\/Judge whether the head node is not NULL\n if(temp != NULL)\n {\n\t\t\/\/set the header node to the next\n sem->task_queue_head = sem->task_queue_head->next;\n\t\t\n\t\t\/\/Judge whether the head node is NULL\n if(sem->task_queue_head == NULL)\n {\n\t\t\t\/\/set the tail node to NULL\n sem->task_queue_tail = NULL;\n }\n\t\t\/\/wake_up head node\n wake_up_process(temp->task);\n\t\t\n\t\t\/\/free\n\t\tkfree(temp);\n }\n \n \/\/unlock\n mutex_unlock(sem->sem_mutex);\n \n return 0;\n}\n<\/code><\/pre>\n<\/p>\n