systems-programming

Modification to “Implementing an N process barrier using semaphores”

喜你入骨 提交于 2019-12-07 17:36:59
问题 Recently I see this problem which is pretty similar to First reader/writer problem. Implementing an N process barrier using semaphores I am trying to modify it to made sure that it can be reuse and work correctly. n = the number of threads count = 0 mutex = Semaphore(1) barrier = Semaphore(0) mutex.wait() count = count + 1 if (count == n){ barrier.signal()} mutex.signal() barrier.wait() mutex.wait() count=count-1 barrier.signal() if(count==0){ barrier.wait()} mutex.signal() Is this correct? I

Changing a process name in runtime

拜拜、爱过 提交于 2019-12-07 14:41:18
问题 For A.EXE PE file, if the program runs as test mode, I would like to change the process name to "A_TEST.exe". And if the program runs as safe mode, I want to change to "A_SAFE.exe" The file name must be same(A.EXE). Is it possible? 回答1: If "process name" is a name which shows Task Manager - you can change it only from ring0. From ring3 you can only change a default window title. #include <intrin.h> PEB* peb = (PEB*)__readfsdword(0x30); wchar_t newTitle[] = L"NewTitle"; UNICODE_STRING

Does anyone see any problem in this program

旧巷老猫 提交于 2019-12-07 07:53:25
问题 After not getting an answer I liked for this question about chroot, I went and rolled my own solution: #include <unistd.h> #include <sys/types.h> #include <pwd.h> #include <stdio.h> extern char **environ; int main(int argc, char** argv, char** envp) { char* path = "/"; char* name = "nobody"; char* exe = "/bin/false"; struct passwd *pass; if(argc < 4) { printf("Need more args: username chroot exe args...\n"); return 1; } name = argv[1]; path = argv[2]; exe = argv[3]; if(!(pass = getpwnam(name)

Changing a process name in runtime

丶灬走出姿态 提交于 2019-12-05 20:54:38
For A.EXE PE file, if the program runs as test mode, I would like to change the process name to "A_TEST.exe". And if the program runs as safe mode, I want to change to "A_SAFE.exe" The file name must be same(A.EXE). Is it possible? If "process name" is a name which shows Task Manager - you can change it only from ring0. From ring3 you can only change a default window title. #include <intrin.h> PEB* peb = (PEB*)__readfsdword(0x30); wchar_t newTitle[] = L"NewTitle"; UNICODE_STRING newTitleUStr = {sizeof(newTitle), sizeof(newTitle), newTitle}; peb->ProcessParameters->WindowTitle = newTitleUStr;

Modification to “Implementing an N process barrier using semaphores”

南笙酒味 提交于 2019-12-05 20:34:32
Recently I see this problem which is pretty similar to First reader/writer problem. Implementing an N process barrier using semaphores I am trying to modify it to made sure that it can be reuse and work correctly. n = the number of threads count = 0 mutex = Semaphore(1) barrier = Semaphore(0) mutex.wait() count = count + 1 if (count == n){ barrier.signal()} mutex.signal() barrier.wait() mutex.wait() count=count-1 barrier.signal() if(count==0){ barrier.wait()} mutex.signal() Is this correct? I'm wondering if there exist some mistakes I didn't detect. Your pseudocode correctly returns barrier

vfork() system call

◇◆丶佛笑我妖孽 提交于 2019-12-05 09:06:56
I read that the new process created using vfork() system call executes as a thread in the parent's address space and until the child thread doesnot calls exit() or exec() system call, the parent is blocked. So I wrote a program using vfork() system call #include <stdio.h> #include <unistd.h> int main() { pid_t pid; printf("Parent\n"); pid = vfork(); if(pid==0) { printf("Child\n"); } return 0; } I got the output as follows: Parent Child Parent Child Parent Child .... .... .... I was assuming that the return statement must be calling the exit() system call internally so I was expecting the

Implementation of Signals under Linux and Windows?

二次信任 提交于 2019-12-05 02:20:29
问题 I am not new to the use of signals in programming. I mostly work in C/C++ and Python. But I am interested in knowing how signals are actually implemented in Linux (or Windows). Does the OS check after each CPU instruction in a signal descriptor table if there are any registered signals left to process? Or is the process manager/scheduler responsible for this? As signal are asynchronous, is it true that a CPU instruction interrupts before it complete? 回答1: The OS definitely does not process

How to avoid race condition when checking if file exists and then creating it?

情到浓时终转凉″ 提交于 2019-12-04 15:54:45
I'm thinking of corner cases in my code and I can't figure out how to avoid problem when you check if file exists, and if it does not, you create a file with that filename. The code approximately looks like this: // 1 status = stat(filename); if (!status) { // 2 create_file(filename); } Between the call to 1 and 2 another process could create the filename. How to avoid this problem and is there a general solution to this type of problems? They happen often in systems programming. You're supposed to create the file anyway, and let the OS know whether or not you want a new file to be created in

Want to know Windows Clipboard Internals

♀尐吖头ヾ 提交于 2019-12-04 11:25:20
I am interested in learning windows system internals and how things work. I am inclined towards learning system programming on windows. With that context, I am curious to know few things on how windows clipboard internally functions: What precisely happens when we select some text, image etc and press Ctrl + C ? What precisely happens when we we press Ctrl + V in different application? Where exactly the copied data resides? Does the copied data go into kernel mode memory - that is shared across all processes? How the copied data becomes available to a different process? I want to know the

Implementation of Signals under Linux and Windows?

☆樱花仙子☆ 提交于 2019-12-03 16:31:45
I am not new to the use of signals in programming. I mostly work in C/C++ and Python. But I am interested in knowing how signals are actually implemented in Linux (or Windows). Does the OS check after each CPU instruction in a signal descriptor table if there are any registered signals left to process? Or is the process manager/scheduler responsible for this? As signal are asynchronous, is it true that a CPU instruction interrupts before it complete? The OS definitely does not process each and every instruction. No way. Too slow. When the CPU encounters a problem (like division by 0, access to