Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

UNIVERZITET U NOVOM SADU

TEHNIČKI FAKULTET “MIHAJLO pUPIN”


ZRENJANIN
nastavni predmet: OPERATIVNI SISTEMI
predmetni nastavnik: Prof. dr Ljubica Kazi
predmetni asistent: MSc Velibor Premčevski
VEŽBE 11 ČAS
Tema: RAD SA LINUX KOMANDAMA

PRIPREMA VEŽBE

Pokretanje Linux online emulatora

https://bellard.org/jslinux/

Otvara se ekran:

ISPROBAVANJE RADA ONLINE LINUX EMULATORA


OSNOVNE KOMANDE LINUXA
https://fossbytes.com/basic-linux-commands-beginners

1. mkdir
The name says it all. The mkdir command in Linux is used to create a new directory or, if you’re coming from Windows, a
Folder.

mkdir folder name


Where “folder name” is the name of the folder you want to create.

2. echo
Echo is the simplest command in Linux. It is used to display text that is passed after the space in the command.

focusblast@pop-os:~$ echo fossbytes


fossbytes
The echo command might seem useless, but it is used in debugging code and determining the program’s state. If you
know a little bit of programming, think of it as a print statement that only outputs a string.

Python3

print("fossbytes")
>> fossbytes
3. pwd
PWD stands for Print Working Directory. This gives us a quick way of finding out the working directory path.

focusblast@pop-os:~$ pwd
/home/focusblast
4. cd
Cd is an acronym for the change directory. As the name suggests, it is used to temporarily change the directory you’re
working in.

focusblast@pop-os:~$ cd /Desktop
focusblast@pop-osDesktop:~/Desktop$
5. cp
The command cp is used to copy and paste a file to a directory specified as the second argument.

focusblast@pop-os:~/Desktop$ cp new.txt /home/focusblast/Downloads


In the above command, “new.txt” is the file that we copy to the directory “/home/focusblast/Downloads.”
6. mv
The mv command in Linux stands for “Move.” It is used to move files or directories from one place to another. Consider a
file “move.” To move it to the Downloads folder, all we need to do is:

focusblast@pop-os:~/Desktop$ mv move /home/focusblast/Downloads


7. man
The manual page (man) command is like software documentation using which you can know what a particular command
does and how it works:

focusblast@pop-os:~$ man cp

CP(1) User Commands CP(1)

NAME
cp - copy files and directories

SYNOPSIS
cp [OPTION]... [-T] SOURCE DEST
cp [OPTION]... SOURCE... DIRECTORY
cp [OPTION]... -t DIRECTORY SOURCE...

DESCRIPTION
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.

Mandatory arguments to long options are mandatory for short options


too.

-a, --archive
same as -dR --preserve=all

--attributes-only
don't copy the file data, just the attributes

--backup[=CONTROL]
Manual page cp(1) line 1 (press h for help or q to quit)
8. ls
The ls command is used for listing the contents of a directory.

focusblast@pop-os:~$ ls

Downloads Public Documents


snap Templates Videos
Music Pictures VMs
9. cat
The cat command is used to print or merge the content from the line where the first file ends.

focusblast@pop-os:~$ cat file1


The command to merge two files and create a new file or merge them in any two files would be.

cat file1 file2 > file3 #This will merge files in file 1 and file 2 in a new file
Now, the cat command has a lot of commands that we couldn’t fit in this article. You can use the command “man cat” to
learn more about it.

10. rm
The rm command is used to remove files and directories. You’ll need both rm and -r (recursive) to remove a directory.
rm new2.txt #removes the file
sudo rm -rf directory #removes the directory
The f in “-rf” is used to tell rm to ignore files and arguments that don’t exist.

11. zip/unzip
Zip is used to create a new zip file, whereas Unzip is used to unzip zipped files. Here’s how you you Zip and Unzip
commands.

focusblast@pop-os:~$ zip newzipfile.zip file1.txt file2.txt


In the above command, newzipfile.zip is the name of the zipped file in which we’re going to put the two text files file1.txt
and file2.txt.
To extract all the files from a zipped file using the command.

focusblast@pop-os:~$ unzip newzipfile.zip


Replace “newzipfile” with the name of the zipped file you want to extract.

12. wget
wget is a handy command that can help you download files from the internet. Here’s how to use it:

wget "download link"


13. top
Similar to Windows Task Manager, top command shows you the list of running processes and how much CPU is being
used.

focusblast@pop-os:~$ top
14. history
The history command is used to display the commands that you’ve typed before.

focusblast@pop-os:~$ history
15. wc
The wc command is used to count the number of lines (-l), words (-w), bytes (-c), and characters (m).

focusblast@pop-os:~$ wc -l filename.txt
33 filename.txt
16. clear
As the name suggests, clear is used to clear the terminal screen.

focusblast@pop-os:~$ clear
17. passwd
You guessed it right! The passwd command is used to change the password of the user account. To use it, type passwd
followed by the username.

focusblast@pop-os:~$ passwd focusblast


18. chown
The chown command is used to transfer the ownership of files. Let’s assume there’s a file named file1 and you’re user0.
You want to transfer the ownership to user1.

focusblast@pop-os:~$ chown user1 file1.txt


You can also transfer the ownership to root using the command.

sudo chown root file1.txt


19. apt
Apt stands for Advanced Packaging Tool. It is one of the most popular and powerful package managers for
Ubuntu/Debian. For starters, a package manager essentially automates the process of installing and removing
applications.

The following command installs the flameshot application, which is one of the most popular screenshot tools on Linux.

sudo apt install flameshot


20. reboot
The name says it all. Reboot command is used to reboot, shut down, or halt the system.

reboot
21. chmod
The chmod command is used to change the read (-r), write (-w), and execute (-x) instructions of a file. An example of
chmod command would be:

chmod 742 program.sh


Here’s what the numbers mean.

Number Permission Representation

0 No Permissions —

1 Execute Permission –x

2 Write Permission -w-

3 Write and Execute -wx

4 Read Permission r–

5 Read and Execute r-x

6 Read and Write rw-

7 Read, Write, and Execute rwx

Linux permissions

The first number (7) in the above command represents the permissions that you’re giving to the user i.e. Read, Write,
and Execute.

The second digit (4) is the permissions given to the file itself, which, in this case, is “Read Permissions only.”

The third and final digit (2) represents the permissions given to everyone who’s not a part of the group.

22. grep
The grep command is used to search and find text in a file.

grep "fossbytes" text.save

23. locate
Similar to the search command in Windows, the locate command is used to locate files in Linux.

$ locate text.save
/home/focusblast/Desktop/text.save
24. sudo
The only command of all that you’ll end up using the most. The acronym for Sudo is SuperUser Do, using which you can
fiddle with the files that require root permissions.

Mind you, if a file needs root privileges, it’s probably important to the OS. Hence, we suggest not to play around if you
don’t know what you’re doing.
25. hostname
The hostname command is used to know your device name. Additionally, using the -I argument will help you know your
IP address.

$ hostname
pop-os
$ hostname -I
192.1.1.1
26. exit
The exit command can be used to close the terminal quickly.

27. df
Suppose you want to know the space in every disk partition, type df. The default space metric is Kilobytes but, you can
use the argument “-m” to change it to Megabytes.

$ df -m

28. netstat
The netstat command can be used to check the network statistics, interface statistics, routing table information, and
much more.

$ netstat
29. fdisk
The fdisk command will list all the partitions and the information like the partition name, sectors, size, and partition types.
fdisk needs superuser permissions to run.

$ sudo fdisk -l
30. Nano
Nano is my favorite text editor in Linux. If you want to open up a text file, you can type “nano” followed by the “filename”
if you’re in the directory where the file is present to open it up.

When you’re done editing the text file, press the key combination “Ctrl+O” to write the changes and “Ctrl+X” to exit. To
learn more about Nano, you can also go to the help section by pressing “Ctrl+G.”

WIN API

https://learn.microsoft.com/en-us/windows/win32/apiindex/windows-api-list?redirectedfrom=MSDN

Windows API index


• Article
• 05/13/2022
• 5 minutes to read
• 12 contributors
Feedback

The following is a list of the reference content for the Windows application programming interface (API) for desktop and
server applications.

Using the Windows API, you can develop applications that run successfully on all versions of Windows while taking
advantage of the features and capabilities unique to each version. (Note that this was formerly called the Win32 API. The
name Windows API more accurately reflects its roots in 16-bit Windows and its support on 64-bit Windows.)

User interface

The Windows UI API creates and uses windows to display output, prompt for user input, and carry out the other tasks
that support interaction with the user. Most applications create at least one window.

• Accessibility
• Desktop Window Manager (DWM)
• Globalization Services
• High DPI
• Multilingual User Interface (MUI)
• National Language Support (NLS)
• User Interface elements:
o Buttons
o Carets
o Combo Boxes
o Common Dialog Boxes
o Common Controls
o Cursors
o Dialog Boxes
o Edit Controls
o Header Controls
o Icons
o Keyboard Accelerators
o List Boxes
o List-View Controls
o Menus
o Progress Bars
o Property Sheets
o Rich Edit Controls
o Scroll Bars
o Static Controls
o Strings
o Toolbars
o Tooltips
o Trackbars
o Tree-View Controls
• Windows Animation Manager
• Windows Ribbon Framework

Windows environment (Shell)

• Windows Property System


• Windows Shell
• Windows Search
• Consoles

User input and messaging

• User Interaction
oDirect Manipulation
oInk input
oInput Feedback Configuration
oInteraction Context
oPointer Device Input Stack
oPointer Input Messages and Notifications
oRadial controller input
oText Services Framework
oTouch Hit Testing
oTouch Injection
• Legacy User Interaction
o Touch Input
o Keyboard Input
o Mouse Input
o Raw Input
• Windows and Messages:
o Messages and Message Queues
o Windows
o Window Classes
o Window Procedures
o Timers
o Window Properties
o Hooks

Data access and storage

• Background Intelligent Transfer Service (BITS)


• Data Backup
o Backup
o Data Deduplication
o Volume Shadow Copy
o Windows Server Backup
• Data Exchange:
o Clipboard
o Dynamic Data Exchange (DDE)
o Dynamic Data Exchange Management (DDEML)
• Directory Management
• Disk Management
• Distributed File System (DFS)
• Distributed File System Replication
• Extensible Storage Engine
• Files and I/O (Local file system)
• iSCSI Discovery Library API
• Offline Files
• Packaging
• Remote Differential Compression
• Transactional NTFS
• Volume Management
• Virtual Hard Disk (VHD)
• Windows Storage Management
• Windows Data Access Components
o Microsoft Open Database Connectivity (ODBC)
o Microsoft OLE DB
o Microsoft ActiveX Data Objects (ADO)

Diagnostics

The Diagnostics API enable you to troubleshoot application or system problems and monitor performance.

• Application Recovery and Restart


• Debugging
• Error Handling
• Event Logging
• Event Tracing
• Hardware Counter Profiling (HCP)
• Network Diagnostics Framework (NDF)
• Network Monitor
• Performance Counters
• Performance Logs and Alerts (PLA)
• Process Snapshotting
• Process Status (PSAPI)
• Structured Exception Handling
• System Monitor
• Wait Chain Traversal
• Windows Error Reporting (WER)
• Windows Event Log
• Windows Troubleshooting Platform

Graphics and multimedia


The Graphics, multimedia, audio, and video APIs enable applications to incorporate formatted text, graphics, audio, and
video.

• Core Audio
• Direct2D
• DirectComposition
• DirectShow
• DirectWrite
• DirectX
• Graphics Device Interface (GDI)
• GDI+
• Media Streaming
• Microsoft Media Foundation
• Microsoft TV Technologies
• OpenGL
• Monitor Configuration
• Multiple Display Monitors
• Picture Acquisition
• Windows Color System
• Windows Imaging Component (WIC)
• Windows Media Audio and Video Codec and DSP
• Windows Media Center
• Windows Media Format
• Windows Media Library Sharing Services
• Windows Media Player
• Windows Media Services
• Windows Movie Maker
• Windows Multimedia

Devices

• AllJoyn
• Communications Resources
• Device Access
• Device Management
• Enhanced Storage
• Function Discovery
• Image Mastering
• Location
• PnP-X Association Database
• Printing
o Print Spooler
o Print Document Package
o Print Schema Specification
o Print Ticket
o XPS Print
• Sensors
• System Event Notification Service (SENS)
• Tool Help
• UPnP
• Web Services on Devices
• Windows Image Acquisition (WIA)
• Windows Media Device Manager
• Windows Portable Devices

System services

The System Services APIs give applications access to the resources of the computer and the features of the underlying
operating system, such as memory, file systems, devices, processes, and threads.

• COM
• COM+
• Compression API
• Distributed Transaction Coordinator (DTC)
• Dynamic-Link Libraries (DLLs)
• Help API
• Interprocess Communications:
o Mailslots
o Pipes
• Kernel Transaction Manager (KTM)
• Memory Management
• Operation Recorder
• Power Management
• Remote Desktop Services
• Processes
• Services
• Synchronization
• Threads
• Windows Desktop Sharing
• Windows System Information
o Handle and Objects
o Registry
o Time
o Time Provider

Security and identity

The Security and Identity APIs enable password authentication at logon, discretionary protection for all sharable system
objects, privileged access control, rights management, and security auditing.

• Authentication
• Authorization
• Certificate Enrollment
• Cryptography
• Cryptographic Next Generation (CNG)
• Directory Services
o Active Directory Domain Services
o Active Directory Service Interfaces (ADSI)
• Extensible Authentication Protocol (EAP)
• Extensible Authentication Protocol Host (EAPHost)
• MS-CHAP Password Management
• Network Access Protection (NAP)
• Network Policy Server Extensions (NPS)
• Parental Controls
• Security WMI Providers
• TPM Base Services (TBS)
• Windows Biometric Framework

Application installation and servicing

• Games Explorer
• Side-by-side Assemblies
• Packaging, deployment, and query APIs
• Developer License
• Restart Manager
• Windows Installer

System admin and management

The System administration interfaces enable you to install, configure, and service applications or systems.
• Boot Configuration Data WMI Provider
• Failover Clusters
• File Server Resource Manager (FSRM)
• Group Policy
• Microsoft Management Console (MMC) 2.0
• NetShell
• Settings Management Infrastructure
• Software Inventory Logging
• Software Licensing
• Restart Manager
• Settings Management Infrastructure
• System Restore
• System Shutdown
• Task Scheduler
• User Access Logging
• Windows Virtual PC
• Microsoft Virtual Server
• Network Load Balancing Provider
• Windows Defender WMI v2
• Windows Deployment Services
• Windows Genuine Advantage
• Windows Management Infrastructure
• Windows Management Instrumentation (WMI)
• Windows Remote Management
• Windows Resource Protection
• Windows Server Update Services
• Windows System Assessment Tool
• Windows Update Agent

Networking and internet

The Networking APIs enable communication between applications over a network. You can also create and manage
access to shared resources, such as directories and network printers.

• Domain Name System (DNS)


• Dynamic Host Configuration Protocol (DHCP)
• Fax Service
• Get Connected Wizard
• HTTP Server
• Internet Connection Sharing and Firewall
• IP Helper
• IPv6 Internet Connection Firewall
• Management Information Base
• Message Queuing (MSMQ)
• Multicast Address Dynamic Client Allocation Protocol (MADCAP)
• Network Address Translation (NAT)
• Network List Manager (NLM)
• Network Management
• Network Share Management
• Peer-to-Peer
• Quality of Service (QOS)
• Remote Procedure Call
• Routing and Remote Access Service (RAS)
• Simple Network Management Protocol (SNMP)
• SMB Management
• Telephony Application Programming Interfaces (TAPI)
• WebDAV
• WebSocket Protocol Component
• Wireless networking:
o Bluetooth
o IrDA
o Mobile Broadband
o Native Wifi
o Windows Connect Now
o Windows Connection Manager
• Windows Filtering Platform
• Windows Firewall with Advanced Security
• Windows HTTP Services (WinHTTP)
• Windows Internet (WinINet)
• Windows Networking (WNet)
• Windows Network Virtualization
• Windows RSS Platform
• Windows Sockets (Winsock)
• Windows Web Services
• XML HTTP Extended Request

Deprecated or legacy APIs

The following are technologies and APIs that are outdated or have been replaced or deprecated from the Windows client
and server operating systems.

• DirectMusic
• DirectSound
• Microsoft UDDI SDK is now included with Microsoft BizTalk Server.
• Network Dynamic Data Exchange (DDE)
• Remote Installation Service: Use Windows Deployment Services instead.
• Virtual Disk Service (VDS): Use Windows Storage Management instead.
• Terminal Services: Use Remote Desktop Services.
• Windows Media Rights Manager
• Windows Messaging (MAPI): Use Office MAPI instead.
• Windows Gadget Platform: Create UWP apps instead.
• Windows Sidebar: Create UWP apps instead.
• Windows SideShow: No replacement.
• WPF Bitmap Effects
WINDOWS.H

https://www.tutorialspoint.com/cprogramming/c_header_files.htm

HEADER FILE

A header file is a file with extension .h which contains C function declarations and macro definitions to be shared
between several source files. There are two types of header files: the files that the programmer writes and the files
that comes with your compiler.
You request to use a header file in your program by including it with the C preprocessing directive #include, like you
have seen inclusion of stdio.h header file, which comes along with your compiler.
Including a header file is equal to copying the content of the header file but we do not do it because it will be error-
prone and it is not a good idea to copy the content of a header file in the source files, especially if we have multiple
source files in a program.
A simple practice in C or C++ programs is that we keep all the constants, macros, system wide global variables, and
function prototypes in the header files and include that header file wherever it is required.

Include Syntax
Both the user and the system header files are included using the preprocessing directive #include. It has the following
two forms −
#include <file>
This form is used for system header files. It searches for a file named 'file' in a standard list of system directories. You
can prepend directories to this list with the -I option while compiling your source code.
#include "file"
This form is used for header files of your own program. It searches for a file named 'file' in the directory containing the
current file. You can prepend directories to this list with the -I option while compiling your source code.

Include Operation
The #include directive works by directing the C preprocessor to scan the specified file as input before continuing with
the rest of the current source file. The output from the preprocessor contains the output already generated, followed
by the output resulting from the included file, followed by the output that comes from the text after
the #include directive. For example, if you have a header file header.h as follows −
char *test (void);
and a main program called program.c that uses the header file, like this −
int x;
#include "header.h"

int main (void) {


puts (test ());
}
the compiler will see the same token stream as it would if program.c read.
int x;
char *test (void);

int main (void) {


puts (test ());
}

Once-Only Headers
If a header file happens to be included twice, the compiler will process its contents twice and it will result in an error.
The standard way to prevent this is to enclose the entire real contents of the file in a conditional, like this −
#ifndef HEADER_FILE
#define HEADER_FILE

the entire header file file

#endif
This construct is commonly known as a wrapper #ifndef. When the header is included again, the conditional will be
false, because HEADER_FILE is defined. The preprocessor will skip over the entire contents of the file, and the
compiler will not see it twice.

Computed Includes
Sometimes it is necessary to select one of the several different header files to be included into your program. For
instance, they might specify configuration parameters to be used on different sorts of operating systems. You could
do this with a series of conditionals as follows −
#if SYSTEM_1
# include "system_1.h"
#elif SYSTEM_2
# include "system_2.h"
#elif SYSTEM_3
...
#endif
But as it grows, it becomes tedious, instead the preprocessor offers the ability to use a macro for the header name.
This is called a computed include. Instead of writing a header name as the direct argument of #include, you simply
put a macro name there −
#define SYSTEM_H "system_1.h"
...
#include SYSTEM_H
SYSTEM_H will be expanded, and the preprocessor will look for system_1.h as if the #include had been written that
way originally. SYSTEM_H could be defined by your Makefile with a -D option.

RAZLIČITI H FAJLOVI

https://www.exefiles.com/en/extensions/h/all-files/alpha/w/
windows.h

is a Windows-specific header file for the C and C++ programming languages which contains declarations for all of the
functions in the Windows API, all the common macros used by Windows programmers, and all the data types used by the
various functions and subsystems.

Child header files

There are a number of child header files that are automatically included with windows.h. Many of these files cannot
simply be included by themselves (they are not self-contained), because of dependencies.

windows.h may include any of the following header files:

• excpt.h – Exception handling


• stdarg.h – variable-argument functions (standard C header)
• windef.h – various macros and types
• winnt.h – various macros and types (for Windows NT)
• basetsd.h – various types
• guiddef.h – the GUID type
• ctype.h – character classification (standard C header)
• string.h – strings and buffers (standard C header)
• winbase.h – kernel32.dll: kernel services; advapi32.dll:kernel services(e.g. CreateProcessAsUser function), access
control(e.g. AdjustTokenGroups function).
• winerror.h – Windows error codes
• wingdi.h – GDI (Graphics Device Interface)
• winuser.h – user32.dll: user services
• winnls.h – NLS (Native Language Support)
• wincon.h – console services
• winver.h – version information
• winreg.h – Windows registry
• winnetwk.h – WNet (Windows Networking)
• winsvc.h – Windows services and the SCM (Service Control Manager)
• imm.h – IME (Input Method Editor)

Extra includes

• cderr.h – CommDlgExtendedError function error codes


• commdlg.h – Common Dialog Boxes
• dde.h – DDE (Dynamic Data Exchange)
• ddeml.h – DDE Management Library
• dlgs.h – various constants for Common Dialog Boxes
• lzexpand.h – LZ (Lempel-Ziv) compression/decompression
• mmsystem.h – Windows Multimedia
• nb30.h – NetBIOS
• rpc.h – RPC (Remote procedure call)
• shellapi.h – Windows Shell API
• wincrypt.h – Cryptographic API
• winperf.h – Performance monitoring
• winresrc.h – used in resources
• winsock.h – Winsock (Windows Sockets), version 1.1
• winspool.h – Print Spooler
• winbgim.h – Standard graphics library

OLE and COM

• ole2.h – OLE (Object Linking and Embedding)


• objbase.h – COM (Component Object Model)
• oleauto.h – OLE Automation
• olectlid.h – various GUID definitions
Macros

Several macros affect the behavior of windows.h.

• UNICODE – when defined, this causes TCHAR to be a synonym of WCHAR instead of CHAR, and all type-generic API
functions and messages that work with text will be defined to the -W versions instead of the -A versions. (It is similar
to the windows C runtime's _UNICODE macro.)
• RC_INVOKED – defined when the resource compiler (RC.EXE) is in use instead of a C compiler.
• WINVER – used to enable features only available in newer operating systems. Define it to 0x0501 for Windows XP,
and 0x0600 for Windows Vista.
• WIN32_LEAN_AND_MEAN – used to reduce the size of the header files and speed up compilation. Excludes things
like cryptography, DDE, RPC, the Windows Shell and Winsock.

PRIMER:

https://riptutorial.com/winapi

#include <windows.h>

int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, PSTR cmdline, int cmdshow)

return MessageBox(NULL, "hello, world", "caption", 0);

Literatura:

https://riptutorial.com/winapi

https://zetcode.com/gui/winapi/

https://www.ibm.com/support/pages/c-api-programming-example

PRIMERI C programa: https://techstudy.org/clanguage/c-programming-example-and-solutions/

https://codeforwin.org/c-programming-examples-exercises-solutions-beginners
ZADATAK:

1. Izvršiti posebno obeležene žuto Linux komande u okviru Linux online emulatora i rezultate dostaviti u sledećoj
tabeli:

LINUX KOMANDA ZNAČENJE EKRAN LINUX EMULATORA SA


REZULTATOM RADA

2. Dat je program napisan primenom windows.h biblioteke, kojom se ilustruje sinhronizacija niti. Objasniti sve
ključne reči C jezika (npr. HANDLE, Char, int, DWORD, WINAPI, void…) iz primera kratko – sa 1-2 rečenice.

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <winbase.h>
#include <time.h>

HANDLE hSemaphorePrviSahista, hSemaphoreDrugiSahista;

char nazivKonzole[80];

int duzinaTrajanjaPartije = 60;


int trenutnoVreme = 0;

DWORD WINAPI PrviSahistaThread(LPVOID lpParam);


DWORD WINAPI DrugiSahistaThread(LPVOID lpParam);

void generisiPotez(int sahistaId);

int main()
{
SetConsoleTitle("Partija saha, sinhronizacija niti demo");

DWORD dwPrviSahistaThreadId, dwDrugiSahistaThreadId;


HANDLE hPrviSahistaThread, hDrugiSahistaThread;

hSemaphorePrviSahista = CreateSemaphore(NULL, 1, 1, NULL);


hSemaphoreDrugiSahista = CreateSemaphore(NULL, 0, 1, NULL);

hPrviSahistaThread = CreateThread(NULL, 0, PrviSahistaThread, (LPVOID)1, 0, &dwPrviSahistaThreadId);


hDrugiSahistaThread = CreateThread(NULL, 0, DrugiSahistaThread, (LPVOID)2, 0, &dwDrugiSahistaThreadId);

while(trenutnoVreme < duzinaTrajanjaPartije)


{
sprintf(nazivKonzole, "Proteklo je %d sekundi", trenutnoVreme);
SetConsoleTitle(nazivKonzole);
Sleep(1000);
trenutnoVreme++;
}

CloseHandle(hSemaphorePrviSahista);
CloseHandle(hSemaphoreDrugiSahista);
CloseHandle(hPrviSahistaThread);
CloseHandle(hDrugiSahistaThread);
return 0;
}

DWORD WINAPI PrviSahistaThread(LPVOID lpParam)


{
int id = (int)lpParam;
while(1)
{
WaitForSingleObject(hSemaphorePrviSahista, INFINITE);

Sleep(random(1,10) * 100);
generisiPotez(id);

ReleaseSemaphore(hSemaphoreDrugiSahista, 1, NULL);
}
}

DWORD WINAPI DrugiSahistaThread(LPVOID lpParam)


{
int id = (int)lpParam;
while(1)
{
WaitForSingleObject(hSemaphoreDrugiSahista, INFINITE);

Sleep(random(1,10) * 100);
generisiPotez(id);

ReleaseSemaphore(hSemaphorePrviSahista, 1, NULL);
}
}

void generisiPotez(int id)


{
printf("Igrac %d je pomerio figuru sa pozicije %c%d na poziciju %c%d....\n",
id,
random('A','H'),
random(1,8),
random('A','H'),
random(1,8));
}

int random(int donjaGranica, int gornjaGranica)


{
srand(time(NULL));
Sleep(200);
return donjaGranica + rand() % (gornjaGranica - donjaGranica + 1);
}

You might also like