Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // SPO-2.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <windows.h>
- #include <conio.h>
- #include <process.h>
- #include <locale.h>
- #define MAX_COUNT 12
- #define BUFFER_SIZE 10
- PROCESS_INFORMATION CreateNewProcess(TCHAR* AppName, TCHAR *cmd);
- void Parent(TCHAR *AppName);
- void Child(int);
- void TryPrint(HANDLE hProcCanWrite, char* str);
- int _tmain(int argc, TCHAR* argv[])
- {
- setlocale(LC_ALL, ".1251");
- if(argc == 2){
- Child( _ttoi(argv[1]) );
- }
- else{
- Parent(argv[0]);
- }
- return 0;
- }
- void Parent(TCHAR *AppName)
- {
- HANDLE hProcCanWrite = CreateEvent(NULL, FALSE, TRUE, TEXT("ProcCanWrite"));
- char input;
- int currentNumberOfProcesses = 0;
- PROCESS_INFORMATION processesArray[MAX_COUNT];
- while( input = _getch() )
- {
- if(input == '-' && currentNumberOfProcesses) {
- TCHAR buffer[BUFFER_SIZE] = {0};
- _itot_s(currentNumberOfProcesses-1, buffer, 10);
- HANDLE hTerminateSignal = OpenEvent( EVENT_ALL_ACCESS, FALSE, buffer);
- SetEvent(hTerminateSignal);
- if (WaitForSingleObject(processesArray[currentNumberOfProcesses-1].hProcess, INFINITE) == WAIT_OBJECT_0 ){
- CloseHandle(processesArray[currentNumberOfProcesses-1].hProcess);
- CloseHandle(processesArray[currentNumberOfProcesses-1].hThread);
- currentNumberOfProcesses--;
- CloseHandle(hTerminateSignal);
- }
- continue;
- }
- if((input == '+') && (currentNumberOfProcesses < MAX_COUNT ))
- {
- TCHAR buffer[BUFFER_SIZE] = {0};
- _itot_s(currentNumberOfProcesses, buffer, 10);
- processesArray[currentNumberOfProcesses] = CreateNewProcess( AppName, buffer);
- HANDLE hTerminateSignal = CreateEvent( NULL, FALSE, FALSE, buffer);
- currentNumberOfProcesses++;
- continue;
- }
- if(input == 'q'){
- break;
- }
- }
- for(int i = 0; i < currentNumberOfProcesses; i++)
- {
- TerminateProcess(processesArray[i].hProcess, 0);
- CloseHandle(processesArray[i].hProcess);
- CloseHandle(processesArray[i].hThread);
- }
- }
- TCHAR* createArgsString(TCHAR* app, TCHAR* arg)
- {
- TCHAR args[100] = {0};
- _tcscat_s( args, app);
- _tcscat_s( args, TEXT(" "));
- _tcscat_s( args, arg);
- return args;
- }
- PROCESS_INFORMATION CreateNewProcess(TCHAR* AppName, TCHAR *cmd)
- {
- STARTUPINFO cif;
- ZeroMemory(&cif,sizeof(STARTUPINFO));
- PROCESS_INFORMATION pi;
- if(!CreateProcess(
- NULL,
- createArgsString(AppName, cmd),
- NULL,
- NULL,
- TRUE,
- NULL,
- NULL,
- NULL,
- &cif,
- &pi )
- )
- {
- puts("Create process failed!");
- }
- return pi;
- }
- char* getTextString(int number){
- static char outputStringArray[MAX_COUNT][150] = {
- "Nirvana ", // was an American rock band that was formed by singer/guitarist Kurt Cobain and bassist Krist Novoselic ",
- "Queen ", //recorded the album A Kind of Magic, containing several reworkings of songs written for the fantasy action film Highlander. ",
- "Scorpions ", //are a rock band from Hannover, Germany formed in 1965 by guitarist Rudolf Schenker. ",
- "Metallica ", //has released nine studio albums, three live albums, five extended plays, 24 music videos, and 45 singles. ",
- "Bon Jovi ", //is an American rock band from Sayreville, New Jersey. ",
- "The Final Countdown ",// is the third studio album by the Swedish hard rock band Europe. Released on 26 May 1986. ",
- "Rammstein ", //was founded by guitarist Richard Z. Kruspe. In 1989, he escaped to West Berlin and started the band Orgasm Death Gimmicks. ",
- "Louna ", //is a Russian Alternative/Punk rock band, formed in Moscow in 2008 by Tracktor Bowling's musicians Lusine Gevorkyan and Vitaly Demidenko ",
- "Sonic Syndicate ",// is a heavy metal band from Falkenberg, Sweden. ",
- "Skillet ",//is an American Christian rock band formed in Memphis, Tennessee in 1996. ",
- "Надежда Никитична Кадышева ",//(род. 1 июня 1959, Горки, Лениногорский район, Татарская АССР) — российская певица эрзянского происхождения. ", // easter egg %)
- "The Beatles "//are unquestionably the best and most important band in rock history, as well as the most compelling story. "
- };
- return outputStringArray[number];
- }
- void Child( int number )
- {
- TCHAR buffer[BUFFER_SIZE] = {0};
- _itot_s(number, buffer,10);
- HANDLE hProcCanWrite = OpenEvent(EVENT_ALL_ACCESS, FALSE, TEXT("ProcCanWrite") );
- HANDLE hTerminateSignal = OpenEvent( EVENT_ALL_ACCESS, FALSE, buffer);
- while( WaitForSingleObject( hTerminateSignal, 10) != WAIT_OBJECT_0 )
- {
- TryPrint(hProcCanWrite, getTextString(number));
- }
- CloseHandle(hProcCanWrite);
- CloseHandle(hTerminateSignal);
- }
- void TryPrint(HANDLE hProcCanWrite, char* str)
- {
- if(WaitForSingleObject( hProcCanWrite, 10) == WAIT_OBJECT_0)
- {
- ResetEvent(hProcCanWrite);
- for ( int i = 0; i < strlen(str); ++i)
- {
- printf_s("%c", str[i] );
- Sleep(50);
- }
- SetEvent(hProcCanWrite);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement